Griffith
Griffith

Reputation: 43

libcurl HTTP request set content-disposition and content-type in MIME data

I'm trying to upload an image on twitter using libcurl, I used the twurl command line tool to generate an HTTP request and see how it should look like, what I get is this:

POST /1.1/media/upload.json HTTP/1.1
Accept: */
Content-Type: multipart/form-data, boundary="00Twurl342528555775455418lruwT99"
Authorization: OAuth oauth_body_hash="XXX", oauth_consumer_key="XXX", oauth_nonce="XXX", oauth_signature="XXX", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1603308767", oauth_token="XXX", oauth_version="1.0"
Connection: close
Host: upload.twitter.com
Content-Length: 612739
--00Twurl342528555775455418lruwT99
Content-Disposition: form-data; name="media"; filename="image.png"
Content-Type: application/octet-stream


binary data of image.png

--00Twurl342528555775455418lruwT99--

The request that I can generate via libcurl (got it using curl verbose) for the moment is this one:

POST /1.1/media/upload.json HTTP/2
Host: upload.twitter.com
accept: */*
authorization: OAuth oauth_consumer_key="XXX",oauth_nonce="XXX",oauth_signature="XXX",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1603372043",oauth_token="XXX",oauth_version="1.0"
content-length: 268
content-type: multipart/form-data; boundary=------------------------d1b0fc28e693c24a

Using the following code:

curl_mime     *mime = nullptr;
curl_mimepart *part = nullptr;

mime = curl_mime_init(request_handle);
part = curl_mime_addpart(mime);

curl_mime_name(part, "media");
curl_mime_filename(part, "image.png");

curl_easy_setopt(request_handle, CURLOPT_MIMEPOST, mime);

The problem is that I don't know how to make my request similar to the first one with libcurl, how do I specify Content-Type and Content-Disposition ?

Edit: solution

Full code

curl_mime*     mime = nullptr;
curl_mimepart* part = nullptr;

/* initialize mime part */
mime = curl_mime_init(request_handle);
part = curl_mime_addpart(mime);

/* content-disposition: form-data; name="media"; filename="image.png" */
curl_mime_name(part, "media");
curl_mime_filename(part, "image.png");

/* add file content */
curl_mime_filedata(part, "image.png");

/* content-type: application/octet-stream */
curl_mime_type(part, "application/octet-stream");

/* link the MIME data to your curl handle */
curl_easy_setopt(request_handle, CURLOPT_MIMEPOST, mime);

I didn't do it to highlight the functions to use, but check function return.

Upvotes: 1

Views: 1509

Answers (1)

rustyx
rustyx

Reputation: 85481

how do I specify Content-Type and Content-Disposition ?

Just read the fine manual (which you can navigate to from the fine example postit2.c)

CURLcode curl_mime_type(curl_mimepart * part, const char * mimetype);

curl_mime_type sets a mime part's content type.

CURLcode curl_mime_filename(curl_mimepart * part, const char * filename);

curl_mime_filename sets a mime part's remote file name. When remote file name is set, content data is processed as a file, whatever is the part's content source. A part's remote file name is transmitted to the server in the associated Content-Disposition generated header.

The official libcurl tutorial is also a nice read.

Upvotes: 0

Related Questions