带有标题的发布请求中的错误411(长度必需),但标题具有Content-Length。的libcurl

时间:2011-09-02 14:58:50

标签: c++ post libcurl

我使用这个选项:

curl_easy_setopt(curl, CURLOPT_URL, urlUpload); 
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);

必须是带标题的帖子请求。在标头变量中有长度

***
char sizeStr[50];
sprintf(sizeStr, "Content-Length: %d", body.length());

*** 

header = curl_slist_append(header, sizeStr);

***

我要做的是将视频上传到YouTube,我正在使用their manual

我收到这样的错误。

<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<title>Error 411 (Length Required)!!1</title>
<style>
  *many symbols here* 
</style>
<a href=//www.google.com/ id=g><img src=//www.google.com/images/logo_sm.gif alt=Google></a>
<p><b>411.</b> <ins>Thatв€™s an error.</ins>
<p>POST requests require a <code>Content-length</code> header. <ins>Thatв€™s all we know.</ins>

也许我必须使用其他一些CURLoptions?

更新 当我设置

curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

我也收到了

* 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=DQAAAIkAAACTK9tZPCTY1XQvXGkg4qkaIuZ1QO-Bh6-    ZyzOHuigFNC_gR4Piep4NljAjdOP4s-k7vqj-j4LdckXM9jxzlElgtaxr-    CShI1vIWkjm5ZtFsj3a9v1YqFmjIkEi3NCP2ON18D9jmXSIarPqprWiOK0n3oxznCBhm4osXwJ1yRstVVM5bG5mOlC331eMCrOKp3E


GData-Version: 2

X-GData-Key:     key=AI39si59VMkm6DATDmfG_Df6D23jfto3xRVfbAEMrFBv035pdRZ5AYMPsRXbGLCRXXnK5jz6KCSWSkuXOTrlDIIKWy7Le9fkQQ

Slug: screen.avi

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

Content-length: 910273

Connection: close


* HTTP 1.0, assume close after body
< HTTP/1.0 411 Length Required

< Content-Type: text/html; charset=UTF-8

< Content-Length: 11791

< Date: Fri, 02 Sep 2011 16:09:58 GMT

< Server: GFE/2.0

< 

* Closing connection #0

2 个答案:

答案 0 :(得分:3)

此错误是因为我从YouTube收到的身份验证字符串最后是新的行符号,我删除它并且此错误消失了。

答案 1 :(得分:0)

CURLOPT_POSTFIELDS将使libcurl自动添加内容长度,无需自定义内容。但是,由于您已经发送了一个内容长度标题显然不是服务器正在讨论的缺失长度。

您的请求还会发送“连接:关闭”,因此您还没有向我们展示更多代码。