libcurl:发送表单数据时将content-disposition设置为attachemnt

时间:2018-09-26 10:22:18

标签: c curl libcurl curl-multi

我正在使用下面的测试代码来更新给定网址上对象的参数“名称”。

如果我从命令行运行curl,它将更新对象:

curl -d "name=rock code" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://petstore.swagger.io:80/v2/pet/7867

但是如果我使用下面的C代码,我会从服务器获得200 OK,但数据不会更新。网上的数据包看起来像代码下面给出的输出。

#include <stdio.h>
#include <string.h>
#include <curl/curl.h>

int main(void)
{
CURL *curl;
CURLcode res = CURLE_OK;
struct curl_slist *headers = NULL;
struct curl_slist *recipients = NULL;
struct curl_slist *slist = NULL;
curl_mime *mime;
curl_mime *alt;
curl_mimepart *part;
const char **cpp;

curl = curl_easy_init();
if(curl) {

curl_easy_setopt(curl, CURLOPT_URL, "http://petstore.swagger.io:80/v2/pet/7867");
curl_mime *mime = NULL;
struct curl_slist *headers = NULL;

headers = curl_slist_append(headers,"Content-Type: application/x-www-form-urlencoded");

curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST,"POST");

mime = curl_mime_init(curl);


curl_mimepart *part = curl_mime_addpart(mime);
curl_mime_name(part, "name");
curl_mime_data(part, "Rock code", CURL_ZERO_TERMINATED);

curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); // to get curl debug msg  

res = curl_easy_perform(curl);

if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));

curl_slist_free_all(headers);

curl_easy_cleanup(curl);

curl_mime_free(mime);
}

return (int)res;
}

数据包:

Hypertext Transfer Protocol
    POST /v2/pet/7867 HTTP/1.1\r\n
        [Expert Info (Chat/Sequence): POST /v2/pet/7867 HTTP/1.1\r\n]
        Request Method: POST
        Request URI: /v2/pet/7867
        Request Version: HTTP/1.1
    Host: petstore.swagger.io\r\n
    Accept: */*\r\n
    Content-Length: 151\r\n
        [Content length: 151]
    Content-Type: application/x-www-form-urlencoded; boundary=------------------------4ad7539e8d91d033\r\n
    \r\n
    [Full request URI: http://petstore.swagger.io/v2/pet/7867]
    [HTTP request 1/1]
    [Response in frame: 43506]
    File Data: 151 bytes
HTML Form URL Encoded: application/x-www-form-urlencoded
    Form item: "--------------------------4ad7539e8d91d033
Content-Disposition: attachment; name" = ""name"

Rockey code
--------------------------4ad7539e8d91d033--
"
        Key: --------------------------4ad7539e8d91d033\r\nContent-Disposition: attachment; name
        Value: "name"\r\n\r\nRockey code\r\n--------------------------4ad7539e8d91d033--\r\n

我不明白为什么要将内容处置作为附件?

应该是附件还是应该是表单上载的表单数据?

还是我对代码有任何错误?如果您有任何想法,请告诉我。

0 个答案:

没有答案