Reputation: 159
I have cURL https request I am trying to send to my web server in a C++ program. I am getting back a "Bad Request" format response.
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl)
{
int res = 0;
snprintf(curl_url, sizeof(curl_url), "https://%s:8080/hello", results);
snprintf(curl_fields, sizeof(curl_fields),"\"name\":\"%s\", \"key\":\"%s\"", name, data);
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, "charset: utf-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, curl_url);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, curl_fields);
res = curl_easy_perform(curl);
if ( res )
{
---error---
}
curl_easy_cleanup(curl);
curl_global_cleanup();
}
Can I get some help ?
Upvotes: 3
Views: 10945
Reputation: 3914
Are you trying to send the following json?
{
"name": name,
"data": data
}
from this line of code
snprintf(curl_fields, sizeof(curl_fields),"\"name\":\"%s\", \"key\":\"%s\"", name, data);
shouldn't it then be
snprintf(curl_fields, sizeof(curl_fields),"{\"name\":\"%s\", \"key\":\"%s\"}", name, data);
(add the curly braces)
Upvotes: 2