1

I am sending a HTTP POST request using python socket:

import socket

s = socket.socket(
    socket.AF_INET, socket.SOCK_STREAM)

s.connect(("127.0.0.1", 4001))

s.send(b'POST /session HTTP/1.1\r\nContent-Type:application/json\r\nUser-Agent: test/1\r\n\r\n{"desiredCapabilities": {}, "capabilities":{}}')
response = s.recv(10000)
print(response)

Output:

b'HTTP/1.1 200 OK\r\ncontent-length: 270\r\nContent-Type: application/json; charset=utf-8\r\nConnection: close\r\n\r\n{"sessionId":"72418bd14689c1cd9ee48706eada96a4","status":33,"value":{"message":"session not created exception: Missing or invalid capabilities\\n  (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Linux 4.4.0-91-generic x86_64)"}}'

Summary of output - error message is returned saying that the json object I am trying to send is missing or not correct.

This is why I decided to capture the request with mitmproxy.

enter image description here

It has no content, while it should have the following json content:

{"desiredCapabilities": {}, "capabilities":{}}

I can't understand what the problem is. I followed the HTTP specification - after each header I put CRLF(\r\n) and after the last header(User-Agent) I put additional CRLF since there must be 1 empty row before body content.

Wiki HTTP message body

The request/status line and headers must all end with <CR><LF> (that is, a carriage return followed by a line feed). The empty line must consist of only <CR><LF> and no other whitespace.

CuriousGuy
  • 1,545
  • 3
  • 20
  • 42

1 Answers1

1

The POST request you send is missing the Content-length header, i.e. the request you send is invalid. Due to a missing Content-length header the server is probably unable to know where the body ends and might assume that there is no body at all, i.e. far from the expected. This is also reflected in your comment: "In summary response is saying that the json object is missing".

I recommend to use an existing HTTP library instead of trying to use socket directly. HTTP is more complex than most developers think.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • Man, thank you so much! This is just awesome. The whole day I was wondering what was going on.. It worked after adding `Content-Length`! – CuriousGuy Aug 15 '17 at 16:44
  • *The whole day I was wondering what was going on* - using a well tested HTTP library could have saved you lots of time then and might probably save more time in the future. Note that with the request you've send you need to be able to deal with HTTP keep alive and chunked responses in addition to simple responses because these features are enabled implicitly by using HTTP version 1.1. Again, HTTP might look simple but it's not. – Steffen Ullrich Aug 15 '17 at 16:47
  • Yes, I already did it using a library. But I wanted to try it on a lower level :) – CuriousGuy Aug 15 '17 at 16:47
  • 1
    @CuriousGuy: In addition, take a look at [this](https://stackoverflow.com/questions/6686261/what-at-the-bare-minimum-is-required-for-an-http-request) – President James K. Polk Aug 15 '17 at 17:47