嘗試使用 Powerdns API 呼叫創建新的 DNS 區域時,我總是收到相同的錯誤(創建域“example.com”失敗)(或任何域)。
我的請求:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://127.0.0.1:953/api/v1/servers/localhost/zones');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"name\":\"example.com.\", \"kind\":\"Native\", \"masters\": [], \"nameservers\":[\"ns1.example.com.\", \"ns2.example.com.\"]}");
$headers = array();
$headers[] = 'X-Api-Key: MY-KEY';
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
用于獲取 DNS 區域串列的類似 api 呼叫正在作業。GET 請求正常作業,但不能 POST。有人可以幫忙嗎?
我的 pdns.conf 檔案是:
api=yes
api-key=MY-KEY
也許我必須更改我的 powerdns 設定中的某些內容,idk 真的。如果有任何幫助,我將不勝感激!
POST 請求后的服務器回應為 422 錯誤(無法處理的物體)
HTTP/1.1 422 無法處理的物體訪問控制允許來源:* 連接:關閉內容長度:52 內容安全策略:default-src 'self';style-src 'self' 'unsafe-inline' 內容型別:application/json 服務器:PowerDNS/4.4.1 X-Content-Type-Options:nosniff X-Frame-Options:拒絕 X-Permitted-Cross-Domain-Policies :無 X-Xss-保護:1;模式=塊
This is my pdns.conf file
cat /etc/pdns/pdns.conf
bind-ignore-broken-records=yes
setuid=named
setgid=named
launch=bind
log-dns-queries=yes
loglevel=5
bind-config=/etc/named.conf
bind-dnssec-db=/var/cpanel/pdns/dnssec.db
local-address-nonexist-fail=no
distributor-threads=1
disable-axfr=yes
webserver=yes
api=yes
webserver-address=127.0.0.1
webserver-allow-from=0.0.0.0/0
webserver-password=SERVER-KEY
#gmysql-dnssec=no
webserver-port=953
api-key=MY-KEY
upgrade-unknown-types=1
uj5u.com熱心網友回復:
您的代碼看起來正確。
您的回復存在一個大問題。
內容長度:52
應該是Content-Length: 111
.
我一直在使用兩個應用程式測驗您的代碼
一個用于發送 curl 請求
一個用于接收它并以請求詳細資訊進行回應。
在我洗掉 的測驗早期Content-Type: application/json
,我會看到內容長度剛剛超過 50 個位元組。
下面是我的應用程式。它得到 111 個位元組。
如果你將 curl url 更改為http://eatled.com/receiveheader.php
你應該得到這個:
Response
Content-Length: 111
Content-Type: application/json
Accept: application/json
Accept-Encoding: deflate, gzip, br
Host: eatled.com
X-Api-Key: MY-KEY
BODY
{"name":"example.com.", "kind":"Native", "masters": [], "nameservers":["ns1.example.com.", "ns2.example.com."]}
array (
'name' => 'example.com.',
'kind' => 'Native',
'masters' =>
array (
),
'nameservers' =>
array (
0 => 'ns1.example.com.',
1 => 'ns2.example.com.',
),
)
鏈接到我的 CURL 沙盒
源代碼:
sandbox.php
<?php
header('Content-Type: text/plain; charset=UTF-8');
$jsn = "{\"name\":\"example.com.\", \"kind\":\"Native\", \"masters\": [], \"nameservers\":[\"ns1.example.com.\", \"ns2.example.com.\"]}";
$data = json_decode($post,1);
$post = $jsn;
$request = array();
$request[] = "Content-Type: application/json";
$request[] = "Accept: application/json";
$request[] = "X-Api-Key: MY-KEY";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://eatled.com/receiveheader.php');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
$data = curl_exec($ch);
$info = rawurldecode(var_export(curl_getinfo($ch),true));
echo "\nResponse\n$data";
echo "\ncurl_getinfo =\n$info";
?>
接收頭檔案.php
<?php
header('Content-Type: text/plain; charset=UTF-8');
foreach (getallheaders() as $name => $value) {
echo "$name: $value\n";
}
echo "\nBODY\n";
$jsn = file_get_contents('php://input');
echo "\n$jsn\n\n\n";
$data = json_decode($jsn,1);
var_export($data);
echo "\n\$_POST\n";
var_export($_POST);
echo "\n\$_FILES\n";
var_export($_FILES);
echo "\n\$_SERVER\n";
var_export($_SERVER);
?>
更新結束
添加這兩個標題。
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/json';
現在你得到的錯誤是因為請求內容型別是
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
如果 use 只使用了 content-type 標頭而不接受,你會得到一個不同的錯誤。API手冊說你不能使用curl的默認值Accept: */*
uj5u.com熱心網友回復:
您可以嘗試在正在運行的 pDNS 網路服務器上使用 curl 命令呼叫 API 嗎?
curl -v \
-H 'Content-Type: application/json' \
-H 'X-Api-Key: MY-KEY' \
-H 'Accept: application/json' \
-X POST -d '{"name":"example.com.", "kind":"Native", "masters": [], "nameservers":["ns1.example.com.", "ns2.example.com."]}' \
http://127.0.0.1:953/api/v1/servers/localhost/zones
因為我不知道你的設定,所以我猜它與網路有關。如果curl
請求作業,您應該檢查webserver-allow-from
配置,您可以嘗試將其設定為接受所有(0.0.0.0,::
)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/507062.html