简体   繁体   English

libcurl HTTP 请求在 MIME 数据中设置 content-disposition 和 content-type

[英]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:我正在尝试使用libcurl在 twitter 上上传图像,我使用twurl命令行工具生成 HTTP 请求并查看它的外观,我得到的是:

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:目前我可以通过 libcurl 生成的请求(使用 curl verbose 得到它)是这样的:

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 ?问题是我不知道如何使用 libcurl 使我的请求类似于第一个请求,如何指定Content-TypeContent-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.我这样做不是为了突出要使用的函数,而是检查函数返回。

how do I specify Content-Type and Content-Disposition ?如何指定Content-TypeContent-Disposition

Just read the fine manual (which you can navigate to from the fine example postit2.c )只需阅读精美的手册(您可以从精美的示例postit2.c导航到该手册

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

curl_mime_type sets a mime part's content type . curl_mime_type设置 mime 部分的内容类型

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

curl_mime_filename sets a mime part's remote file name. curl_mime_filename设置 mime 部分的远程文件名。 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.部件的远程文件名在关联的Content-Disposition生成的标头中传输到服务器。

The official libcurl tutorial is also a nice read.官方的 libcurl教程也很不错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM