title: JSON requests in C using libcurl
body: <p>I'm defining a PUT request with a JSON request body using libcurl in C.</p>
  
  <p>This how I'm doing it:</p>
  
  <pre><code>    sprintf(jsonObj, "\"name\" : \"%s\", \"age\" : \"%s\"", name, age);
  
      struct curl_slist *headers = NULL;
      curl_slist_append(headers, "Accept: application/json");
      curl_slist_append(headers, "Content-Type: application/json");
      curl_slist_append(headers, "charset: utf-8");
  
      curl_easy_setopt(curl, CURLOPT_URL, url);
  
      curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
      curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); 
      curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonObj);
      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
      curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&amp;chunk);
      curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcrp/0.1");
  
      res = curl_easy_perform(curl);
  </code></pre>
  
  <p>The request body is arriving like this:</p>
  
  <pre><code>    { '"name" : "Pedro", "age" : "22"' }
  </code></pre>
  
  <p>With <code>{ '</code> at the start and <code>' }</code> at the end.</p>
  
  <p>--- MORE INFO -----</p>
  
  <p>if I declare this code</p>
  
  <pre><code>    char* jsonObj = "{ \"name\" : \"Pedro\" , \"age\" : \"22\" }"; 
  
      struct curl_slist *headers = NULL;
      curl_slist_append(headers, "Accept: application/json");
      curl_slist_append(headers, "Content-Type: application/json");
      curl_slist_append(headers, "charset: utf-8");
  
      curl_easy_setopt(curl, CURLOPT_URL, url);
  
      curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
      curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); 
      curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonObj);
      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
      curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&amp;chunk);
      curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcrp/0.1");
  
      res = curl_easy_perform(curl);
  </code></pre>
  
  <p>the server receives this as the request body:</p>
  
  <pre><code>{ '{ "name" : "Pedro" , "age" : "22" }': '' }
  </code></pre>
  
  <p>My question is:</p>
  
  <p>Is the libCurl pre-formatting/encoding automatically the Json request?</p>
  
  <p>By the way, does the libCurl have some way of encoding a JSON object?  </p>
  
  <p>Thanks so much!</p>