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
- Contemporary messages sorted: [ by date ] [ by thread ] [ by subject ] [ by author ] [ by messages with attachments ]
From: Taw via curl-library <curl-library_at_lists.haxx.se>
Date: Thu, 23 Jun 2022 09:08:13 +0000 (UTC)
Hello,
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 https://curl.se/libcurl/c/postit2.html, 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
Date: Thu, 23 Jun 2022 09:08:13 +0000 (UTC)
Hello,
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 https://curl.se/libcurl/c/postit2.html, 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
-- Unsubscribe: https://lists.haxx.se/listinfo/curl-library Etiquette: https://curl.se/mail/etiquette.htmlReceived on 2022-06-23