Reda Reda
Reda Reda

Reputation: 13

Read HTTP POST request from file

I'm trying to read HTTP post request stored in a file then send it via python socket, the problem is that post body is not read by the webapp, is there any trick to cosider when sending HTTP requests from a low-level ?

Code snippet :

f = open('req.txt', 'rb')
req = f.read()
f.close()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, 80))
s.send(req)
s.close()

req.txt :

POST /api HTTP/1.1
Host: mywebsite.com
Connection: close
Cookie: nothing
Content-Lenght: 11

param=value

Upvotes: 1

Views: 750

Answers (1)

abarnert
abarnert

Reputation: 365925

You misspelled Content-Length.

According to HTTP/1.1 (RFC7230), given that this is a request message, and does not have a Transfer-Encoding header, having no Content-Length header means that the body length is 0.

The rules are actually a bit more complicated than that (follow the link), and many servers bend over backward to be tolerant of clients that break the rules—but this is probably why the webapp framework is ignoring the body rather than passing it on to your app.

Upvotes: 2

Related Questions