Home > Back-end >  how do i POST an empty array with curl in c?
how do i POST an empty array with curl in c?

Time:01-27

When trying to send an empty array using curl the array get's received as so emptyArr['']

with quote instead of being posted as an empty emptyArr[]

how do i post the emptyArr without the quote?

#include <curl/curl.h>

CURL *curl;
      CURLcode res;
    
    curl_global_init(CURL_GLOBAL_ALL);
   
    /* get a curl handle */
    curl = curl_easy_init();
    if(curl) {
      /* First set the URL that is about to receive our POST. This URL can
         just as well be a https:// URL if that is what should receive the
         data. */
      curl_easy_setopt(curl, CURLOPT_URL, "http://someaddress.com");
      /* Now specify the POST data */
      curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "emptyArr[]");
   
      /* Perform the request, res will get the return code */
      res = curl_easy_perform(curl);
      /* Check for errors */
      if(res != CURLE_OK)
        fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));
   
      /* always cleanup */
      curl_easy_cleanup(curl);
    }
    curl_global_cleanup();

CodePudding user response:

Empty arrays don't really exist in URL-encoded parameters. When you send an array, it's sent as:

name[]=firstElement&name[]=secondElement&name[]=thirdElement

An empty array means you don't send any of these, but then there's no parameter at all.

It's the responsibility of the server code to handle the nonexisting parameter and treat it as an empty array.

When you write

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "emptyArr[]");

it's being treated as

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "emptyArr[]=");

so you're creating an array with one element whose value is an empty string.

You should simply leave out the parameter entirely, and the server should treat it as empty.

CodePudding user response:

gona answer my own question so basically for posting and empty array you have to include the headers

to indicate that the request body format is JSON

struct curl_slist *list = NULL;
list = curl_slist_append(list, "content-type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);

the code will look such as

don't forget to add the escape sequence (reverse-slash, backslash, anti-slash) or whatever you call it before the double quote \" \"

#include <curl/curl.h>

CURL *curl;
      CURLcode res;
    
    curl_global_init(CURL_GLOBAL_ALL);
   
    struct curl_slist *list = NULL;
    
    curl = curl_easy_init();
    if(curl) {

      list = curl_slist_append(list, "content-type: application/json");
    
      curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
      curl_easy_setopt(curl, CURLOPT_URL, "http://someaddress.com");
      /* Now specify the POST data */
      curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"emptyArr\":[]}"); //<-- reverse-slash before the double quote \" \"
   
      /* Perform the request, res will get the return code */
      res = curl_easy_perform(curl);
      /* Check for errors */
      if(res != CURLE_OK)
        fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));
   
      /* always cleanup */
      curl_easy_cleanup(curl);
    }
    curl_global_cleanup();
  •  Tags:  
  • Related