2

I am trying to send a post request through the request module with headers["Transfer-encoding"] = "chunked", but I am getting back:

<BODY><h2>Bad Request - Invalid Content Length</h2><hr><p>HTTP Error 400. There is an invalid content length or chunk length in the request.</p>

I am sending a json string. headers["Content-Type"] = "application/json" is also given. Does anybody know if I am missing some setting? Maybe I should set the chunk-size somewhere?

Analysing the headers of the request attached to the response I actually get a content-length header different from zero.

I also tried to create a custom generator from the json string, and pass it to the post method as data=, but it it seems to simply hang there (also above the given timeout=).

Neo
  • 448
  • 7
  • 18

1 Answers1

4

Your error says you didn't create the request properly (it's 4xx error, not 5xx which would indicate server issue). Transfer-Encoding: chunked serves for sending data in chunks. When the body of your message consists of unspecified number of chunks and you send them in lets say - stream. I would suggest reading this.

Each chunk should have it's size in front of the data. For instance:

HTTP/1.1 200 OK 
Content-Type: text/plain 
Transfer-Encoding: chunked

9\r\n
Some data\r\n 
6\r\n
Python\r\n

If you want to send chunked requests with python requests module. You probably need a generator method for that. Please see this. With such few information I can't help you more.

Kraxi
  • 93
  • 2
  • 12