curl / Mailing Lists / curl-library / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

Upload multipart with cURL & json & custom headers

From: Taw via curl-library <>
Date: Thu, 23 Jun 2022 09:08:13 +0000 (UTC)

I am trying to do a multipart/form-data upload with cURL + json + custom headers and I am not sure how to do it properly. I want something equivalent to this cmdline command:
curl -H 'Content-Type: multipart/form-data' -H 'x-custom-header: custom value' https://url_to_upload -F "file=_at_file_to_upload" -F "params=_at_params.json"

For simple POST requests, I am doing this and works:
curl_slist_append(headers, "Content-type: application/json")curl_slist_append(headers, "x-custom-header: custom value")curl_easy_setopt(CURLOPT_HTTPHEADER, headers);curl_easy_setopt(CURLOPT_POSTFIELDS, json_str);
I read, but still not sure how to send the json and the headers for multipart upload.
1. For JSON should I use CURLOPT_POSTFIELDS or curl_mime_addpart/curl_mime_name/curl_mime_data?2. For headers should I use CURLOPT_HTTPHEADER or curl_mime_headers?3. Can I reuse a mime handle for multiple files? Is there any way to reset it, so I won't do curl_mime_init/curl_mime_free for each file?
thank you

Received on 2022-06-23