【问题标题】:libcUrl Bad request. Seem that POST body isn't sent, only headerlibcUrl 错误请求。似乎没有发送 POST 正文,只有标题
【发布时间】:2011-09-07 13:04:07
【问题描述】:

我使用这个选项:

curl_easy_setopt(curl, CURLOPT_URL, urlUpload);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, body.length());
curl_easy_setopt(curl, CURLOPT_HEADER, 1);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str());

我正在尝试按照their manual 将文件上传到 Youtube,我的请求的标题和正文与手册中的完全相同。

我收到错误请求,详细模式显示如下:

* About to connect() to uploads.gdata.youtube.com port 80 (#0)
*   Trying 74.125.XX.XXX... * connected
* Connected to uploads.gdata.youtube.com (74.125.XX.XXX) port 80 (#0)
> POST /feeds/api/users/default/uploads HTTP/1.1

Accept: */*

Host: uploads.gdata.youtube.com

Authorization: GoogleLogin    auth=D *** c

GData-Version: 2

X-GData-Key: key=A *** Q

Slug: screen.avi

Content-Type: multipart/related; boundary="d31fcjR2"

Content-Length: 910273

Connection: close

Expect: 100-continue



* Done waiting for 100-continue
< HTTP/1.1 400 Bad Request

< Server: Upload Server Built on Aug 29 2011 16:45:26 (1314661526)

< Content-Type: text/plain; charset=utf-8

< Date: Wed, 07 Sep 2011 12:57:51 GMT

< Pragma: no-cache

< Expires: Fri, 01 Jan 1990 00:00:00 GMT

< Cache-Control: no-cache, no-store, must-revalidate

< Content-Length: 11

< Connection: close

< 

* Closing connection #0

似乎由于某种原因根本没有发送正文,有人可以帮助我吗?

更新:

当我禁用 Expect: 标头时,我仍然收到错误请求。

* About to connect() to uploads.gdata.youtube.com port 80 (#0)
*   Trying 74.125.XX.XXX... * connected
* Connected to uploads.gdata.youtube.com (74.125.XX.XXX) port 80 (#0)
> POST /feeds/api/users/default/uploads HTTP/1.1

Accept: */*

Host: uploads.gdata.youtube.com

Authorization: GoogleLogin auth=D *** s

GData-Version: 2

X-GData-Key:    key=A *** Q

Slug: screen.avi

Content-Type: multipart/related; boundary="d31fcjR2"

Content-Length: 910273

Connection: close



< HTTP/1.1 400 Bad Request

< Server: Upload Server Built on Aug 29 2011 16:45:26 (1314661526)

< Content-Type: text/plain; charset=utf-8

< Date: Wed, 07 Sep 2011 13:59:38 GMT

< Pragma: no-cache

< Expires: Fri, 01 Jan 1990 00:00:00 GMT

< Cache-Control: no-cache, no-store, must-revalidate

< Content-Length: 11

< Connection: close

< 

* Closing connection #0

【问题讨论】:

  • 正文未发送,因为服务器未使用 100: Continue 标头进行响应。它没有回答可能是因为它不理解 Expect 标头。尝试禁用它(使用CURLOPT_HTTPHEADER)。
  • @n.m.我禁用了。我也收到了错误请求,我更新了我的问题。
  • 此外,您的换行符似乎有误,或类似情况。看看请求中的 POST 标头是如何在输出中以 &gt; 字符开头的,所有其他行的开头都没有 &gt;。通常,所有标题都应以&gt; 字符开头。所有回复标题前面都有一个 &lt; 字符,就像它们应该的那样。我建议您使用tcpdumpwireshark 来嗅探流量并验证所有行尾是否正确。

标签: c++ post curl libcurl


【解决方案1】:

我的错误是由于 POST 正文中的错误。所以这个 cUrl 选项是用 header 形成 POST 请求的正确方法。

【讨论】:

    猜你喜欢
    • 2016-07-30
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 2019-04-12
    • 2021-03-12
    • 2017-08-31
    • 2017-12-01
    • 1970-01-01
    相关资源
    最近更新 更多