简体   繁体   English

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

[英]JSON request using cURL in C++

I have following command in cURL, this works fine in terminal. 我在cURL中有以下命令,在终端中可以正常工作。

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

This json api sends a few parameters like these-- "status":{"code":200,"message":"OK"} 此json api发送一些这样的参数- "status":{"code":200,"message":"OK"}

Now i want my c++ program to execute it. 现在我希望我的C ++程序执行它。 I have set up and used cURL before for ftp upload and download from ftp examples. 我已经设置并使用cURL进行ftp上传和从ftp示例下载。 But i did not find any example to do this. 但是我没有找到任何这样做的例子。

I want to know how can i pass username and password parameters to the json api, and get response from it. 我想知道如何将用户名和密码参数传递给json api,并从中获取响应。

Here is what i have tried in some code I found on web, it didnt work. 这是我在网上找到的一些代码中尝试过的方法,它没有起作用。

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);
    }
}

How do i get response from the web? 我如何从网上得到回应? i know it will be in the form of strings, and i am capable enough to parse it. 我知道它将以字符串的形式出现,并且我有足够的能力来解析它。

I am looking up all the params (--insecure, -X, POST, --data)passed to the curl command executed on terminal, so as to get little idea about what i have to do. 我正在查找传递给终端上执行的curl命令的所有参数(-不安全,-X,POST,-data),以便对我必须做的事情一无所知。

I am a graphics programmer :) not so good with web services. 我是图形程序员:)对Web服务不太满意。 I'd appreciate any help. 我将不胜感激。

To send post data, you need to tell curl where it is. 要发送帖子数据,您需要告诉curl它在哪里。 Something like: 就像是:

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);

To read the response into memory, it is a bit more complicated - you need to have a callback function that is called to store the data. 要将响应读入内存,要复杂一些-您需要有一个调用回调函数来存储数据。 There is an example of this in the curl docs , although you could just append the results into a std::string, rather than having your own chunk structure like they do. 在curl docs中有一个示例,尽管您可以将结果附加到std :: string中,而不是像它们那样拥有自己的块结构。

a lot depends on how you send the request. 在很大程度上取决于您如何发送请求。

The way you send your data is important. 您发送数据的方式很重要。 if you want a response from server in json format, you should request in json format as well. 如果您希望服务器以json格式进行响应,则也应以json格式进行请求。

Along with what The dark said above, combining it with a JSON request, i got the result i was expecting.. 连同上述Dark所说的内容,将其与JSON请求结合在一起,我得到了预期的结果。

The following code worked for me... the params that you send to the server should be in json format--- 以下代码对我有用...您发送到服务器的参数应为json格式---

std::string data = "{\"username\":\"testuser\",\"password\":\"12345\"}";
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);  // for --insecure option
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, data.length());
curl_easy_setopt(curl, CURLOPT_POST, 1);

The Curl command has an option -libcurl . Curl命令具有-libcurl选项。 It should help you figure out the correct libcurl code to use. 它应该可以帮助您找出要使用的正确libcurl代码。

Add this option to the end of your working Curl command along with a filename and Curl will output a working libcurl c example of your Curl command. 将此选项与文件名一起添加到工作的Curl命令的末尾,Curl将输出Curl命令的工作的libcurl c示例。

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

Compile the outputted code with the -lcurl option. 使用-lcurl选项编译输出的代码。

g++ -lcurl test.cpp -o testcurl g ++ -lcurl test.cpp -o testcurl

Below is an example of the libcurl code I use to POST JSON from c++ to node.js. 以下是我用于将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;

Node.js (Express) receives the JSON as: Node.js(Express)以以下方式接收JSON:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM