Curl 命令行有效,C++ curl 库无效

Curl command line works, C++ curl library does not

我正在尝试通过 C++ 的 curl 库执行一些请求。我可以 成功地 执行我的请求并通过 命令行 获得正确的响应,但我无法通过 C++ 代码获得正确的响应。我的命令行命令看起来像这样

curl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -H 'Authorization: <some_hash_value>' -k <my_full_url> -data '<my_json_string>'

效果很好。现在我尝试在 C++ 代码中执行相同的请求。我的代码看起来像这样

void performRequest(const std::string& json, const void* userData, CallbackFunction callback)
{
    struct curl_slist* headers = NULL;

    headers = curl_slist_append(headers, "Accept: application/json");
    headers = curl_slist_append(headers, "Content-Type: application/json");
    headers = curl_slist_append(headers, (std::string("Authorization: ") + m_authorization).c_str());

    CURL* curlHandle = curl_easy_init();
    if (!curlHandle)
    {
        std::cerr << "Curl handler initialization failed";
    }

    curl_easy_setopt(curlHandle, CURLOPT_NOSIGNAL, 1);
    curl_easy_setopt(curlHandle, CURLOPT_HTTPHEADER, headers);

    // specify target URL, and note that this URL should include a file name, not only a directory
     curl_easy_setopt(curlHandle, CURLOPT_URL, m_url.c_str());

    // enable uploading
    curl_easy_setopt(curlHandle, CURLOPT_UPLOAD, 1L);

    // set HTTP method to POST
    curl_easy_setopt(curlHandle, CURLOPT_CUSTOMREQUEST, "POST");

    // set json data; I use EXACTLY the same string as in command line
    curl_easy_setopt(curlHandle, CURLOPT_COPYPOSTFIELDS, json.c_str());

    // set data size
    curl_easy_setopt(curlHandle, CURLOPT_POSTFIELDSIZE_LARGE, json.size());

    // set user data for getting it in response
    curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, userData);    // pointer to a custom struct

    // set callback function for getting response
    curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, callback);    // some callback

    // send request
    curl_easy_perform(curlHandle);

    curl_easy_cleanup(curlHandle);
    curl_slist_free_all(headers);
}

但是,由于某种原因,我在服务器的响应中收到一个错误,从中我可以假设我的代码请求不等同于命令行命令。似乎没有发送 body。当我使用 CURLOPT_DEBUGFUNCTION 转储调试信息时,我看不到我的请求 Json 正文。

这里有什么问题?我究竟做错了什么?有什么想法吗?

这是适合您的示例代码。

注意我:

  • 删除了 CURLOPT_UPLOAD,因为它看起来不像您实际上传的东西,而只是做一个简单的 POST.

  • 已将 CURLOPT_CUSTOMREQUEST 更改为 CURLOPT_POST(这并不重要),但我发现它更干净。

  • 重新排序 CURLOPT_POSTFIELDSIZE_LARGECURLOPT_COPYPOSTFIELDS

  • 为了该示例代码删除了 CURLOPT_WRITEDATA 行。

我仅通过连接到 nc -l localhost 80

的实例测试了以下内容
 static size_t callback(char *ptr, size_t size, size_t nmemb, void *userdata)
 {
   string s(ptr);
   cout << s << endl;
   return size * nmemb;
 }


 int main(int argc, char** argv)
 {
   string m_authorization("PWNED");
   string m_url("http://localhost");
   string m_json("{}");

   curl_global_init(CURL_GLOBAL_ALL);

   CURL* curlHandle = curl_easy_init();

   struct curl_slist* headers = nullptr;
   headers = curl_slist_append(headers, "Accept: application/json");
   headers = curl_slist_append(headers, "Content-Type: application/json");
   headers = curl_slist_append(headers, (std::string("Authorization: ") + m_authorization).c_str());

   curl_easy_setopt(curlHandle, CURLOPT_NOSIGNAL, 1);
   curl_easy_setopt(curlHandle, CURLOPT_HTTPHEADER, headers);

   // specify target URL, and note that this URL should include a file name, not only a directory
   curl_easy_setopt(curlHandle, CURLOPT_URL, m_url.c_str());

   // <= You are not uploading anything actually, this is a simple POST with payload
   // enable uploading
   // curl_easy_setopt(curlHandle, CURLOPT_UPLOAD, 1L);

   // set HTTP method to POST
   curl_easy_setopt(curlHandle, CURLOPT_POST, 1L);

   // set data size before copy
   curl_easy_setopt(curlHandle, CURLOPT_POSTFIELDSIZE_LARGE, m_json.size());

   // set json data; I use EXACTLY the same string as in command line
   curl_easy_setopt(curlHandle, CURLOPT_COPYPOSTFIELDS, m_json.c_str());

   // set user data for getting it in response
   // curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, userData);    // pointer to a custom struct

   // set callback function for getting response
   curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, callback);    // some callback

   // send request
   curl_easy_perform(curlHandle);

   curl_slist_free_all(headers);

   curl_easy_cleanup(curlHandle);
   curl_global_cleanup();
   return 0;
 }

根据 Rocki's and drew010 的评论,问题已解决。

  1. 我删除了 CURLOPT_CUSTOMREQUESTCURLOPT_UPLOADCURLOPT_NOSIGNAL 设置语句,因为不需要它们。
  2. 我也删除了设置 CURLOPT_POSTFIELDSIZE_LARGE 的行,尽管如果在设置 CURLOPT_COPYPOSTFIELDS 之前设置它就可以正常工作。 如果在 CURLOPT_COPYPOSTFIELDS 之前未设置大小,则假定数据为零终止字符串;否则,存储的大小会通知库要复制的字节数。在任何情况下,在 CURLOPT_COPYPOSTFIELDS 之后不得更改大小,除非发出另一个 CURLOPT_POSTFIELDSCURLOPT_COPYPOSTFIELDS 选项。 (参见:curl.haxx.se/libcurl/c/CURLOPT_COPYPOSTFIELDS.html

在 windows 中,您必须使用以下函数初始化 winsock 内容

curl_global_init(CURL_GLOBAL_ALL);