Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send raw POST request using Socket

I am trying to send a raw POST request to a chromedriver server.

Here is what I try to initiate a new session:

import socket

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

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

s.send(b'POST /session HTTP/1.1\r\nContent-Type:application/json\r\n{"capabilities": {}, "desiredCapabilities": {}}\r\n\r\n')
response = s.recv(4096)
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":"b26166c2aac022566917db20260500bb","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 error: json object I am sending is not getting parsed correctly

When I use the same json object but send it by requests library, everything is OK:

import requests

params = {
        'capabilities': {},
        'desiredCapabilities': {}
    }


headers = {'Content-type': 'application/json'}

URL = "http://127.0.0.1:9515"

r = requests.post(URL + "/session", json=params)

print("Status: " + str(r.status_code))
print("Body: " + str(r.content))

Output:

Status: 200
Body: b'{"sessionId":"e03189a25d099125a541f3044cb0ee42","status":0,"value":{"acceptSslCerts":true,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"browserName":"chrome","chrome":{"chromedriverVersion":"2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8)","userDataDir":"/tmp/.org.chromium.Chromium.LBeQkw"},"cssSelectorsEnabled":true,"databaseEnabled":false,"handlesAlerts":true,"hasTouchScreen":false,"javascriptEnabled":true,"locationContextEnabled":true,"mobileEmulationEnabled":false,"nativeEvents":true,"networkConnectionEnabled":false,"pageLoadStrategy":"normal","platform":"Linux","rotatable":false,"setWindowRect":true,"takesHeapSnapshot":true,"takesScreenshot":true,"unexpectedAlertBehaviour":"","version":"60.0.3112.90","webStorageEnabled":true}}'

Summary of output: json object is parsed successfully by the chromedriver and new session is created

Do you, guys, have an idea why sending the raw POST request using socket is not working as expected?

like image 419
CuriousGuy Avatar asked Aug 15 '17 14:08

CuriousGuy


1 Answers

There are several issues about your HTTP request:

  • The body of the HTTP request should be separated from the head by \r\n\r\n.
  • You need the Content-Length field, otherwise the remote host does not know when your body is complete.
  • The Host field is mandatory in HTTP 1.1. (Since you receive 200 with your first request, your server probably doesn't insist.)

I've got your example to work (with an apache webserver) by using:

s.send(b'POST /session HTTP/1.1\r\nHost: 127.0.0.1:9515\r\nContent-Type: application/json\r\nContent-Length: 47\r\n\r\n{"capabilities": {}, "desiredCapabilities": {}}')

To be visually more clear, the valid HTTP request looks like

POST /session HTTP/1.1
Host: 127.0.0.1:9515
Content-Type: application/json
Content-Length: 47

{"capabilities": {}, "desiredCapabilities": {}}
like image 54
sauerburger Avatar answered Oct 18 '22 14:10

sauerburger