0

What i want to achieve is to be able to upload a file of any size and also send post data in the same request. The server is guaranteed to be able to handle chunked encoding and also support unlimited size of size.

To achieve that I used CURLOPT_HTTPOST and set the header Transfer-Encoding: chunked, The problem is that I'm am not sure if libcurl is actually using chunked encoding at all. In my attempts to try and figure out I set CURLOPT_VERBOSE to 1 and implemented a custom debug callback to monitor the http headers. From curl I can only see 1 HTTP Request and only 1 HTTP Response (code 200). I didn't see any http code 201 responses.

My question is how do I enable chunk encoding for file uploads in multipart POST requests? If what I'm doing is correct why does libcurl not show me all the requests, how do I make sure it works like it is required?

Bellow is a short example of what I am doing in code.

//Assume `curl` has been initialised 
struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
struct curl_slist *headerlist=NULL;

curl_formadd(&formpost, 
             &lastptr, 
             CURLFORM_COPYNAME, 
             "foo", 
             CURLFORM_COPYCONTENTS, 
             "bar", 
             CURLFORM_END);

curl_formadd(&formpost, 
             &lastptr, 
             CURLFORM_COPYNAME, 
             "file", 
             CURLFORM_FILE, 
             "filepath", 
             CURLFORM_FILENAME, 
             "filename", 
             CURLFORM_END);

headerlist = curl_slist_append(headerlist, "Transfer-Encoding: chunked");

curl_easy_setopt(curl, CURLOPT_URL, "https://foo/uploadfile");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_to_string);
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, curl_progress_cb);
curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &progress_id);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);


CURLcode res = curl_easy_perform(curl);

Thanks in advance

Phenom
  • 223
  • 1
  • 8
  • Why do you think you need to use chunked encoding for this? – Barmar Mar 20 '17 at 17:46
  • 2
    A request sent with chunked encoding is still just a single request. It just uses chunks instead of sending a `Content-Length:` header. Chunked encoding has nothing to do with the `201 Continue` response. – Barmar Mar 20 '17 at 17:48
  • @Barmar There is a size limit on the server side if chunked encoding is not used. So what I'm experiencing is correct behavior? I'm a little confused for its purpose and misunderstood how it works. Do you happen to know what is the default transfer encoding of libcurl? Thank you very much for your help. – Phenom Mar 20 '17 at 18:12
  • There's an example of using chunked encoding here: https://gist.github.com/p120ph37/9490dc9a8a1b5a38b9a2 Your code looks similar, so I expect it should work. But there's no reason to expect to see more than one request, that's not how chunked encoding works. – Barmar Mar 20 '17 at 18:20

0 Answers0