C ++ cUrl将mulipart / form-data文件发送到webserver

时间:2016-07-12 05:42:22

标签: c++ curl

就像标题所说我想将一个带有cUrl lib的文件在C ++程序中发送到网络服务器。

我从服务器获取了网址,并获得了它想要HTTP POST方法和mutipart / form-data的信息。它还想要一个包含数据的参数文件。

我还有一个与curl控制台一起使用的cUrl调用:

  

curl -X POST -H“Cache-Control:no-cache”-H“Content-Type:multipart / form-data”-F“file=@test.txt”“url”

如果我运行下面的代码,服务器会给出没有上传或没有多部分表单的答案。

现在我的代码:

//## File stuff
struct stat file_info;
FILE *fd;

fopen_s(&fd,"file.txt", "rb"); /* open file to upload */
if (!fd) {

    return 1; /* can't continue */
}

/* to get the file size */
if (fstat(_fileno(fd), &file_info) != 0) {

    return 1; /* can't continue */
}
//##

CURL *hnd = curl_easy_init();

curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_URL, "url");

struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "cache-control: no-cache");
headers = curl_slist_append(headers, "content-type: multipart / form-data");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);

curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(hnd, CURLOPT_READDATA, fd);

curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE,(curl_off_t)file_info.st_size);

CURLcode ret = curl_easy_perform(hnd);

修改

包含缓冲区文件的新代码:

std::string contents;
std::ifstream in("empty.txt", std::ios::in | std::ios::binary);

if (in)
{
    in.seekg(0, std::ios::end);
    contents.resize(in.tellg());
    in.seekg(0, std::ios::beg);
    in.read(&contents[0], contents.size());
    in.close();
}

CURL *hnd = curl_easy_init();

uint32_t size = contents.size();

//struct curl_slist *headers = NULL;
//headers = curl_slist_append(headers, "cache-control: no-cache");
//headers = curl_slist_append(headers, "content-type: multipart/form-data");
//curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);

curl_easy_setopt(hnd, CURLOPT_URL, "url");
curl_easy_setopt(hnd, CURLOPT_POST, 1);
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, contents.data());
curl_easy_setopt(hnd, CURLOPT_POSTFIELDSIZE, size);

CURLcode ret = curl_easy_perform(hnd);

现在服务器响应没有文件数据。我还要说它是多部分/表格数据吗?但是,如果我取消注释标头设置,则响应是关于多形式边界的错误。 我也对所需的“文件”参数感到困惑。我是否必须将其添加到CURLOPT_POSTFIELDS?

希望你们中的一位能帮助我,谢谢!

编辑最终

我明白了。以下代码适用于给定服务器设置。

std::string contents;
std::ifstream in("file.txt", std::ios::in | std::ios::binary);

if (in)
{
    in.seekg(0, std::ios::end);
    contents.resize(in.tellg());
    in.seekg(0, std::ios::beg);
    in.read(&contents[0], contents.size());
    in.close();
}
//##


CURL *curl;
CURLcode res;

struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
struct curl_slist *headerlist = NULL;
static const char buf[] =  "Expect:";

curl_global_init(CURL_GLOBAL_ALL);

// set up the header
curl_formadd(&formpost,
    &lastptr,
    CURLFORM_COPYNAME, "cache-control:",
    CURLFORM_COPYCONTENTS, "no-cache",
    CURLFORM_END);

curl_formadd(&formpost,
    &lastptr,
    CURLFORM_COPYNAME, "content-type:",
    CURLFORM_COPYCONTENTS, "multipart/form-data",
    CURLFORM_END);

curl_formadd(&formpost, &lastptr,
    CURLFORM_COPYNAME, "file",  // <--- the (in this case) wanted file-Tag!
    CURLFORM_BUFFER, "data",
    CURLFORM_BUFFERPTR, contents.data(),
    CURLFORM_BUFFERLENGTH, contents.size(),
    CURLFORM_END);

curl = curl_easy_init();

headerlist = curl_slist_append(headerlist, buf);
if (curl) {

    curl_easy_setopt(curl, CURLOPT_URL, "url");

    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
    //curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    //curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
    //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    res = curl_easy_perform(curl);
    /* Check for errors */
    if (res != CURLE_OK)
        fprintf(stderr, "curl_easy_perform() failed: %s\n",
            curl_easy_strerror(res));


    curl_easy_cleanup(curl);


    curl_formfree(formpost);

    curl_slist_free_all(headerlist);
}

2 个答案:

答案 0 :(得分:0)

您的命令行正在使用POST,但您的代码使用CURLOPT_UPLOAD,这显然是不同的。

您应该使用CURLOPT_POST代替,并且应该将文件读取(或映射)到缓冲区中:

// Assume you read or map your file into 
// `uint8_t* buffer`, with `uint32_t size`
...
CURL *hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_URL, "url");
curl_easy_setopt(hnd, CURLOPT_POST, 1);
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, buffer);
curl_easy_setopt(hnd, CURLOPT_POSTFIELDSIZE, size);

答案 1 :(得分:0)

您无法正确格式化多部分数据。在netcat实用程序的帮助下,您可以看到它应该是什么样子:

在终端1:

$ nc -lk 8080 # listen for TCP connection on port 8080

在终端2:

$ curl -X POST -F "file=@test.txt" localhost:8080

通过检查终端1,您将看到curl发送了类似于以下内容的请求:

POST / HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.43.0
Accept: */*
Content-Length: 193
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------a975b09b8e15800c

--------------------------a975b09b8e15800c
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain

qwerty

--------------------------a975b09b8e15800c--

现在,通过将数据发送到netcat(localhost:8080)并修复代码直到其请求类似于curl发送的代码来调试C ++代码。

一般建议 - 不要盲目调试。您无法控制的服务器的响应无法提供有关该问题的足够信息。如果可能,请在允许您查看数据的每一部分的环境中进行调试,并验证数据是否符合规范和/或您的期望。