2

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 ?

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
ejohnson
  • 139
  • 1
  • 4
  • 16
  • What type is `curl_url`? – Galik Aug 09 '18 at 21:58
  • 1
    Try printing out the values (like your URL) before using them to make sure they are correct. – Galik Aug 09 '18 at 21:59
  • char[256] for both curl_url and curl_fields. – ejohnson Aug 09 '18 at 22:04
  • I have noticed that sometimes servers require a `User-Agent` header. – Galik Aug 09 '18 at 22:18
  • 1
    `charsets` is not a valid HTTP request header. Perhaps you meant `Accept-Charset` instead? – Remy Lebeau Aug 10 '18 at 00:14
  • Possible duplicate of [How do I POST a buffer of JSON using libcurl?](https://stackoverflow.com/questions/8251325/) and [JSON requests in C using libcurl](https://stackoverflow.com/questions/11973994/). – Remy Lebeau Aug 10 '18 at 00:19
  • "Maybe someone that searches for answers will find this post useful. In C#, which is not C++ you can't make a json from a string. In C# You Serialize and Deserialize a JSON. I used a NewtonsoftJSON plugin for that. https://www.newtonsoft.com/json" ~*Original text by [malin](https://stackoverflow.com/users/11152270/malin)* – Hille Mar 05 '19 at 07:14
  • Maybe someone that searches for answers will find this post usefull. In C#, which is not C++ you can't make a json from string. In C# You Serialize and Deserialize a JSON. I used a NewtonsoftJSON plugin for that. https://www.newtonsoft.com/json – malin Mar 05 '19 at 06:55

1 Answers1

2

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)

John Allard
  • 3,564
  • 5
  • 23
  • 42
  • I can try that. But I read that setting the headers formats the json with the curl braces. Is that not right ? – ejohnson Aug 09 '18 at 22:06
  • try it and get back to me, I don't think that is the case. – John Allard Aug 09 '18 at 22:09
  • I will do that. – ejohnson Aug 09 '18 at 22:26
  • Yes, you have to include the curly braces yourself, curl will not do it for you. Also, if you are using C++11 or later, you might consider using a [Raw string literal](https://en.cppreference.com/w/cpp/language/string_literal) to make the format string a little easier to read: `snprintf(curl_fields, sizeof(curl_fields), R"({"name":"%s", "key":"%s"})", name, data);` – Remy Lebeau Aug 10 '18 at 00:25
  • @RemyLebeau - here is the curl command I run in the terminal that works and I am trying to write code in my C++ program to issue that curl request. I get a "Bad Request 400" output. Looks like garbage is sent over. curl --header "Content-Type: application/json" \--request POST \--data '{"key_info": {"john":"4x"}}' \http://xxx.xx.xx.xx:8080/hello ... Can you please help me out with my C++ code ? – ejohnson Aug 11 '18 at 00:04
  • John Allard - That did not help. But if possible, can you check my above comment to @RemyLebeau and the curl command from the terminal I am trying to code in my C++ code above ? My output is really bad - xxx.xxx.xxx.xxx - - [11/Aug/2018 00:01:29] code 400, message Bad request version ('ì«\x83»s4ôÍ/¦¾/Ó\x00\x00\x9eÀ0À,À(À$À\x14À') xxx.xxx.xxx.xxx - - [11/Aug/2018 00:01:29] "ü2{^)sË9× ÂÙð³ ì«»s4ôÍ/¦¾/ÓÀ0À,À(À$ÀÀ" HTTPStatus.BAD_REQUEST - – ejohnson Aug 11 '18 at 00:07
  • I am going to try a different snprint and see if that works - snprintf(curl_fields, sizeof(curl_fields),"{\"key_info\": {\"%s\":\"%s\"}}", name, data); . That should hopefully match the curl terminal command ? – ejohnson Aug 11 '18 at 00:21