JSON request using cURL in C ++

I have the following command in cURL, this works fine in the terminal.

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

This json api sends a few of these parameters: "status":{"code":200,"message":"OK"}

Now I want my C ++ program to execute it. I installed and used cURL before downloading and downloading ftp from ftp examples. But I did not find an example for this.

I want to know how to pass username and password parameters to json api and get a response from it.

Here is what I tried in some code that I found on the Internet, it did not 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 to get an answer from the Internet? I know that it will be in the form of strings, and I am capable enough to parse it.

I look at all the parameters (--insecure, -X, POST, --data) passed to the curl command executed on the terminal to get an idea of ​​what I should do.

:) -. .

+4
3

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

, - , . curl, std::string, .

+2

, .

, . json, json.

, , JSON, , .

... , , 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);
+1

Curl -libcurl. libcurl .

Curl , Curl libcurl c Curl.

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

libcurl, POST JSON ++ node.js.

  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) JSON :

{username: 'bob', password: '12345'}

+1
source

Source: https://habr.com/ru/post/1536387/


All Articles