如果我这样做
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);
但那不起作用!
提前谢谢!
答案 0 :(得分:2)
我最终遵循以下示例代码: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);
答案 1 :(得分:0)
这里是官方网站的例子。如何制作多部分请求。 http://curl.haxx.se/libcurl/c/multi-post.html