开发者

Error 411 (Length Required) in post request with header, but header has Content-Length. libCurl

开发者 https://www.devze.com 2023-04-02 15:43 出处:网络
I use this options: curl_easy_setopt(curl, CURLOPT_URL, urlUpload); curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);

I use this options:

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);

It must be post request with header. And in header variable there is length

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

*** 

header = curl_slist_append(header, sizeStr);

***

What I'm trying to do is to upload video to YouTube, I'm using their manual

And I receive such error.

<!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>

Maybe I must use some other CURLoptions?

UPDATE: when I set

curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

I also receive

* About to connect() to uploads.gdata.youtube.com port 80 (#0)
*   Trying 74.125.XX.XXX... * connected
* Connected to uploads.开发者_StackOverflowgdata.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


This error was because in the authentication string that I receive from YouTube was in the end the new line symbol, I erase it and this error disappeared.


CURLOPT_POSTFIELDS will make a content-length get added automatically by libcurl, no need to make a custom one. However, as you're already sending a content-length header that is clearly not the missing length the server is talking about.

Your request also sends a "Connection: close" so there's something more of the code that you didn't show us.

0

精彩评论

暂无评论...
验证码 换一张
取 消