gpt4 book ai didi

c++ - curl:使用 multipart 和 post 参数

转载 作者:太空宇宙 更新时间:2023-11-04 14:19:40 26 4
gpt4 key购买 nike

如果我这样做

curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_params);

服务器将看不到多部分,但如果我注释第二行

curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
//curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_params);

会的。

这里有什么问题吗?

UPD: 那么,现在的问题是,是否可以将值赋给参数,以便该值位于服务器端的 POST 参数数组中?我在努力

    headers = curl_slist_append(headers, "Content-Disposition: form-data");

//action=upload
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "action",
CURLFORM_COPYCONTENTS, "upload",
CURLFORM_CONTENTHEADER, headers,
CURLFORM_CONTENTTYPE, "Content-Type: multipart/form-data",
CURLFORM_END);

但这行不通!

提前致谢!

最佳答案

我最终主要遵循此处的示例代码:http://curl.haxx.se/libcurl/c/multi-post.html

这是基于 curl 示例链接最终为我工作的代码示例:

... 
CURL *curl;
curl_mime *form;

curl = curl_easy_init();

/* Set the headers for the request. */
scoped_curl_slist headers;
headers.list = NULL;
string session_header = "Authorization: Bearer " + token;
headers.list = curl_slist_append(headers.list, session_header.c_str());
headers.list = curl_slist_append(headers.list, "cache-control: no-cache");
headers.list = curl_slist_append(headers.list, "Content-Type: multipart/form-data");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers.list);

curl_easy_setopt(curl, CURLOPT_URL, "your_url.com");

/* Create the form */
form = curl_mime_init(curl);

/* Fill in the file upload field */
curl_mimepart *field = curl_mime_addpart(form);
curl_mime_name(field, "upload");
curl_mime_filedata(field, "C:/file_name.txt");

/* Fill in the filename field */
field = curl_mime_addpart(form);
curl_mime_name(field, "name");
curl_mime_data(field, "file_name.txt", CURL_ZERO_TERMINATED);
curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);

CURLcode result = curl_easy_perform(curl);
...
curl_mime_free(form);
curl_easy_cleanup(curl);
curl_slist_free_all(headers);

关于c++ - curl:使用 multipart 和 post 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8604926/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com