【发布时间】:2018-07-20 08:09:42
【问题描述】:
我有以下 cmd 及其工作。 - 使用摘要认证 - 使用 json 数据
curl -v --digest -u user:passwd -X POST http://192.168.139.65:80/digest/getMode -H "Content-Type: application/json" -d '{"Data":{}}'
我想将 cURL 转换为 libcurl。
curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.139.65:80/digest/getMode");
headerlist = curl_slist_append(headerlist, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_easy_setopt(curl, CURLOPT_USERPWD, "user:passwd ");
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"Data\":\t{}\r\n}");
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
但是我失败了,它返回401。有没有遗漏?感谢您的帮助
【问题讨论】:
-
您的选项似乎正确。但是,“passwd”后面有一个空格。它不应该是“user:passwd”而不是“user:passwd”吗?另外,我认为使用
CURLOPT_CUSTOMREQUEST是多余的——因为使用CURLOPT_POSTFIELDS应该自动将请求类型设置为POST。