错误411长度必需c ++,libcurl PUT请求 [英] error 411 Length Required c++, libcurl PUT request

查看:1044
本文介绍了错误411长度必需c ++,libcurl PUT请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使我设置在头Content-Lenght我得到411错误。我正在尝试发送PUT请求。
struct curl_slist * headers = NULL;

Even though I set in header Content-Lenght I'm getting 411 error. I'm trying to send PUT request. struct curl_slist *headers = NULL;

curl = curl_easy_init();
std::string paramiters =
        "<data_file><archive>false</archive><data_type_id>0a7a184a-dcc6-452a-bcd3-52dbd2a83ea2</data_type_id><data_file_name>backwardstep.stt</data_file_name><description>connectionfile</description><job_id>264cf297-3bc7-42e1-8edc-5e2948ee62b6</job_id></data_file>";

if (curl) {

    headers = curl_slist_append(headers, "Accept: */*");
    headers = curl_slist_append(headers, "Content-Length: 123");
    headers = curl_slist_append(headers, "Content-Type: application/xml");


    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");

    curl_easy_setopt(curl, CURLOPT_URL,
            "..url/data_files/new/link_upload.xml");

    curl_easy_setopt(curl, CURLOPT_USERPWD, "kolundzija@example.ch:PASS");

    curl_easy_setopt(curl, CURLOPT_HEADER, 1L);

    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, paramiters.c_str());
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,
            strlen(paramiters.c_str()));

    curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);

    res = curl_easy_perform(curl);

这是SERVER的回应:

and this is response from SERVER:

Host: cloud...
Transfer-Encoding: chunked
Accept: */*
Content-Length: 123
Content-Type: application/xml
Expect: 100-continue

* The requested URL returned error: 411 Length Required
* Closing connection #0


推荐答案

您不需要使用文件,不要使用自定义请求, PUT选项,因为它在文档中指定:

You DON'T need to use a file and do NOT use custom requests, INstead set the UPLOAD and PUT options as it is specified in the documentation here:

http://curl.haxx.se/libcurl/c/httpput.html

与上述使用档案的范例不同作为您的数据结构,您可以使用任何保存您的数据。它是所有使用回调函数与此选项:
CURLOPT_READFUNCTION
差异是如何设置您的回调函数,只需要做两个事情:
1.以字节为单位测量有效负载(您的数据)的大小
2.将数据复制到curl传递给回调的内存地址(即呼叫的第一个参数返回函数,此定义中的FIRST void指针)

Unlike the example above where they use a file as your data structure you can USE ANYTHING to hold your data.It's all on using a callback function with this option: CURLOPT_READFUNCTION The difference is made on how you set your callback function which only has to do two things: 1.-measure the size of your payload (your data) in bytes 2.-copy the data to the memory address that curl passes to the callback (that is the first argument on your call back function, the FIRST void pointer in this definition)

static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)

这是ptr参数。

使用memcpy复制数据。

Use memcpy to copy the data.

查看此链接。我遇到了和你一样的问题,并能够使用这种方法解决它,你需要记住的一件事是,你还需要设置文件大小之前发送curl请求。

Take a look at this link. I ran into the same problem as you and was able to solve it using this approach,one thing YOU need to keep in mind is that you ALSO need to set the file size before sending the curl request.

如何在libcurl中发送长PUT数据而不使用文件指针?

使用CURLOPT_INFILESIZE或CURLOPT_INFILESIZE_LARGE。

Use CURLOPT_INFILESIZE or CURLOPT_INFILESIZE_LARGE for that.

这篇关于错误411长度必需c ++,libcurl PUT请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆