curl / Mailing Lists / curl-users / Single Mail


Re: Upload image to server Libcurl c++

From: Daniel Stenberg <>
Date: Thu, 16 Feb 2017 12:06:49 +0100 (CET)

On Thu, 16 Feb 2017, Avi wrote:

You rather want to use the curl-library mailing list for libcurl stuff, that's
the list for libcurl users and hackers. This list is more for the users of the
curl tool.

> I want to upload an image from a user's computer to my server.
> I used an app called postman to help me, I successfully got results from
> postman, it generates code for you, I got it to work with CMT curl:
> curl -X POST -H "Cache-Control: no-cache" -H "Postman-Token:
> fb42cc8e-63dc-a079-9889-35088dd7f637" -H "Content-Type:
> multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -F
> "imagefile=@duck.jpg" ""

That's a questionable use of curl (and feel free to report that to postman if
that's what generated it). Why set the Content-Type like that? Remove that
part and it becomes a much better command line. Oh, and remove "-X POST" as
well while you're at it, it will only bring you tears. (I presume your URL
using is made up.)

> curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");

Remove this.

> headers = curl_slist_append(headers, "content-type: multipart/form-data;
> boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");

Remove this.

> curl_easy_setopt(hnd, CURLOPT_POSTFIELDS,
> "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
> name=\"imagefile\"; filename=\"duck.jpg\"\r\nContent-Type:
> image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--");

Don't do that. Use curl_formadd() instead and add each section one by one,
which is what the curl -F option does. See the multi-post.c example:

Received on 2017-02-16