gpt4 book ai didi

linux - 如何使用 libcurl Xively HTTP PUT

转载 作者:太空宇宙 更新时间:2023-11-04 11:02:43 25 4
gpt4 key购买 nike

我想发出 HTTPS PUT 请求来放置一个 csv 文件。下面是用于将数据上传到 Xively 的代码。早些时候我收到 411 length required 错误。我引用了此处可用的代码 ( Send string in PUT request with libcurl ) 来解决此问题,其中生成了 CURLOPT_CUSTOMREQUEST。现在我收到 HTTP 500 内部服务器错误。

void upload()
{
CURL *curl;
CURLcode res;
FILE * hd_src;
struct stat file_info;
struct curl_slist* header = NULL;
char * csvfile = "123.csv";

/* get the file size */
stat(csvfile, &file_info);

hd_src = fopen("123.csv","rb");

curl_global_init(CURL_GLOBAL_ALL);

curl = curl_easy_init();

if(curl)
{

header = curl_slist_append(header,"X-ApiKey: 123123123"); /* API KEY HERE - sample only*/
header = curl_slist_append(header,"Accept: text/csv");
header = curl_slist_append(header, "Host: api.xively.com");

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

/* Actual Xively feed is here. For demonstration purpose the feed is listed as 123 */
curl_easy_setopt(curl, CURLOPT_URL, "https://api.xively.com/v2/feeds/123.csv");

curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);

curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT" );
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, hd_src);

res = curl_easy_perform(curl);

if(res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
}
fclose(hd_src);
curl_slist_free_all(header);
curl_global_cleanup();
}

谁能提出建议?如果我删除 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, hd_src),我会得到 411 length required 错误。我删除了它并使用 file_info.st_size 将 Content-Length 添加到 header 。再次收到 411 长度要求错误。使用 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, hd_src) 411 已解决,但 Xively 给出了 500 个内部服务器错误。

提前致谢。

最佳答案

尝试使用 CURLOPT_UPLOAD相反,像 documentation说:

CURLOPT_PUT - make a HTTP PUT request
...
This option is deprecated since version 7.12.1. Use CURLOPT_UPLOAD!

CURLOPT_UPLOAD - enable data upload
...
If the protocol is HTTP, uploading means using the PUT request unless you tell libcurl otherwise

你必须非常小心 CURLOPT_CUSTOMREQUEST :

CURLOPT_CUSTOMREQUEST - custom string for request
...
When you change the request method by setting CURLOPT_CUSTOMREQUEST to something, you don't actually change how libcurl behaves or acts in regards to the particular request method, it will only change the actual string sent in the request
...
Many people have wrongly used this option to replace the entire request with their own, including multiple headers and POST contents. While that might work in many cases, it will cause libcurl to send invalid requests and it could possibly confuse the remote server badly.

您也没有发送 Content-Type header ,而是发送了 Accept header 。 Content-Type 告诉服务器您发送的数据类型。 Accept 告诉服务器您愿意在回复中接收什么类型的数据。

最后,您没有对 stat()fopen() 进行错误处理。

试试这个:

void upload()
{
CURL *curl;
CURLcode res;
FILE * hd_src;
struct stat file_info = {0};
struct curl_slist* header = NULL;
char * csvfile = "C:\\full path to\\123.csv";

/* get the file size */
if (stat(csvfile, &file_info) == -1)
{
fprintf(stderr, "stat() failed: %s\n", strerror(errno));
return;
}

hd_src = fopen(csvfile, "rb");
if (!hd_src)
{
fprintf(stderr, "fopen() failed: %s\n", strerror(errno));
return;
}

curl_global_init(CURL_GLOBAL_ALL);

curl = curl_easy_init();
if (!curl)
{
fprintf(stderr, "curl_easy_init() failed\n");
}
else
{
header = curl_slist_append(header, "X-ApiKey: 123123123"); /* API KEY HERE - sample only*/
header = curl_slist_append(header, "Content-Type: text/csv");

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

/* Actual Xively feed is here. For demonstration purpose the feed is listed as 123 */
curl_easy_setopt(curl, CURLOPT_URL, "https://api.xively.com/v2/feeds/123.csv");

curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);

curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L );

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(header);
curl_easy_cleanup(curl);
}

fclose(hd_src);

curl_global_cleanup();
}

关于linux - 如何使用 libcurl Xively HTTP PUT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26350307/

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