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