簡體   English   中英

Libcurl身份驗證問題

[英]Libcurl authentication issues

我正在嘗試在C ++中使用libcurl將請求發送到url。 當我在命令行中使用curl設置請求時,它似乎可以正常工作:

curl -vvv -X POST -H "Authorization: <api key here>" -H "Content-Type:application/json" "<host>" --data-binary '<json data here>'

響應開始如下:

> POST <host> HTTP/1.1
> Host: <host>
> User-Agent: curl/7.61.1
> Accept: */*
> Authorization: <api_key>
> Content-Type:application/json
> Content-Length: 80

這樣我可以看到授權已正確發送。

但是,當我嘗試使用libcurl C庫在C ++中執行類似的操作時,我沒有注意到請求標頭前面的“>”:

碼:

    struct curl_slist *chunk = NULL;
    chunk = curl_slist_append(chunk, "Authorization: <api_key>");
    chunk = curl_slist_append(chunk, "Content-Type:application/json");


    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
    curl_easy_setopt(curl, CURLOPT_URL, "<host>");

    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "<json>");
    res =  curl_easy_perform(curl);
    curl_easy_cleanup(curl);

響應:

> POST <host> HTTP/1.1
Host: <host>
Accept: */*
Authentication: <api_key>
Content-Type:application/json
Content-Length: 97

因此,我什至不確定主機是否正確處理或接收了標頭。

有任何想法嗎?

我得到以下回應:

{
  "message": "No authorization header given",
  "code": 401
}

標頭不正確。 應該是授權,而不是身份驗證。

在命令行的詳細輸出中,標題稱為“ Authorization:”。 在您的libcurl詳細信息中,輸出其“身份驗證:”。 授權!=認證?

詳細輸出:

對於命令行和libcurl之間的詳細信息,僅是一種不同的輸出格式。 標頭已發送。 例如,php curl使用相同的輸出格式。 只有第一行具有“>”,然后所有后續標頭都沒有“>”。 但是他們都被提交了。

PHP curl的詳細示例輸出:

*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 80 (#0)
> GET /XXX/api.php HTTP/1.1
Host: localhost
Accept: */*
Authorization: XXX
Content-Type: application/json

< HTTP/1.1 200 OK
< Date: Mon, 31 Dec 2018 20:12:51 GMT
< Server: Apache/2.4.34 (Win32) OpenSSL/1.1.0i PHP/7.2.10
< X-Powered-By: PHP/7.2.10
< Content-Length: 2390
< Content-Type: text/html; charset=UTF-8
< 
* Connection #0 to host localhost left intact

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM