我如何在 c 中 POST 一个带有 curl 的空数组?

how do i POST an empty array with curl in c?

当尝试使用 curl 发送空数组时,收到的数组是这样 emptyArr['']

带引号而不是 posted 作为空 emptyArr[]

如何 post 没有引号的 emptyArr

#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();

URL-encoded 参数中并不真正存在空数组。当你发送一个数组时,它被发送为:

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

空数组意味着您不发送任何这些,但是根本没有参数。

服务器代码负责处理不存在的参数并将其视为空数组。

写的时候

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

它被视为

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

因此您正在创建一个数组,其中一个元素的值为空字符串。

您应该完全省略该参数,服务器会将其视为空。