在C ++中使用cURL进行JSON请求

2am*_*2am 5 c++ json curl

我在cURL中有以下命令,在终端中可以正常工作。

curl --insecure -X POST --data "username=testuser&password=12345" https://m360-prototype.herokuapp.com/sessions.json

这个json api发送一些这样的参数- "status":{"code":200,"message":"OK"}

现在我希望我的C ++程序执行它。我已经设置并使用cURL进行ftp上传和从ftp示例下载。但是我没有找到任何这样做的例子。

我想知道如何将用户名和密码参数传递给json api,并从中获取响应。

这是我在网上找到的一些代码中尝试过的方法,它没有起作用。

struct curl_slist *headers=NULL; // init to NULL is important

headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charsets: utf-8");

curl = curl_easy_init();
if(curl) {
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    curl_easy_setopt(curl, CURLOPT_URL, "https://m360-prototype.herokuapp.com/sessions.json");
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "username=testuser&password=12345");

    curl_easy_setopt(curl, CURLOPT_HTTPGET,1);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    res = curl_easy_perform(curl);

    if(CURLE_OK == res) {
        char *ct;
        /* ask for the content-type */
        res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
        if((CURLE_OK == res) && ct)
            printf("We received Content-Type: %s\n", ct);
    }
}
Run Code Online (Sandbox Code Playgroud)

我如何从网上得到回应?我知道它将以字符串的形式出现,并且我有足够的能力来解析它。

我正在查找传递给终端上执行的curl命令的所有参数(-不安全,-X,POST,-data),以便对我必须做的事情一无所知。

我是图形程序员:)对Web服务不太满意。我将不胜感激。

Rah*_*oja 5

Curl 命令有一个选项 -libcurl 。它应该可以帮助您找出要使用的正确 libcurl 代码。

将此选项与文件名一起添加到工作 Curl 命令的末尾,Curl 将输出 Curl 命令的工作 libcurl c 示例。

curl --insecure -X POST --data "username=testuser&password=12345" https://m360-prototype.herokuapp.com/sessions.json -libcurl test.cpp

使用 -lcurl 选项编译输出的代码。

g++ -lcurl test.cpp -o testcurl

下面是我用来将 JSON 从 c++ POST 到 node.js 的 libcurl 代码示例。

  CURLcode ret;
  CURL *hnd;
  struct curl_slist *slist1;
  std::string jsonstr = "{\"username\":\"bob\",\"password\":\"12345\"}";

  slist1 = NULL;
  slist1 = curl_slist_append(slist1, "Content-Type: application/json");

  hnd = curl_easy_init();
  curl_easy_setopt(hnd, CURLOPT_URL, "http://u/r/l");
  curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
  curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, jsonstr.c_str());
  curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.38.0");
  curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, slist1);
  curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
  curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
  curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);

  ret = curl_easy_perform(hnd);

  curl_easy_cleanup(hnd);
  hnd = NULL;
  curl_slist_free_all(slist1);
  slist1 = NULL;
Run Code Online (Sandbox Code Playgroud)

Node.js (Express) 接收 JSON 为:

{ 用户名:'bob',密码:'12345' }


The*_*ark 2

要发送发布数据,您需要告诉curl它在哪里。就像是:

std::string data = "username=testuser&password=12345";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, data.length());
curl_easy_setopt(curl, CURLOPT_POST, 1);
Run Code Online (Sandbox Code Playgroud)

要将响应读入内存,有点复杂 - 您需要调用一个回调函数来存储数据。在curl 文档中有一个这样的示例,尽管您可以将结果附加到 std::string 中,而不是像他们那样拥有自己的块结构。