简体   繁体   中英

libcurl: Malformed encoding found in chunked-encoding

I'm now trying to get a response from Web REST API, but it failed with the following issue(from libcurl debugging)

Malformed encoding found in chunked-encoding

struct wapi_resp_data {
        char *ptr;
        size_t len;
};

static size_t
write_resp_data(void *data, size_t size, size_t nmemb, void *param)
{
        struct wapi_resp_data *res = (struct wapi_resp_data *) param;

        res->ptr = realloc(res->ptr, res->len + size * nmemb + 1);
        if (!res->ptr) {
                res->len = 0;
                return -1;
        }
        memcpy(&res->ptr[res->len], data, size * nmemb);

        res->len += size * nmemb;
        res->ptr[res->len] = '\0';

        return size * nmemb;
}

int curl_get_response(const char *url, struct curl_slist_headers *headers)
{
   ...

   curl_easy_setopt(curl, CURLOPT_HTTPHEADER, post_headers);
   curl_easy_setopt(curl, CURLOPT_POSTFIELDS, req_data);
   curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(req_data));

   curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_resp_data);

   memset(&res, 0, sizeof(res));
   curl_easy_setopt(curl, CURLOPT_WRITEDATA, &res);
   ...
}

I tried to test with curl command, but it's working.

Here is the example command:

curl -d '{ "test": "test"}' -H "Content-Type: application/json"

http://xxx/api/ -v

Is there any problem in my code?

This is a problem with the received HTTP chunked encoded stream received from the server.

If you're using a very old curl, it might happen because curl was stricter before curl 7.35.0 but since then it tries to be more laxed as long as it understands what's coming.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM