I know there are tons of ways to add headers or cookies something like this.
But what I want to do is to add "\r\n" on the top of the request so as to look like the following body.
Request Body >>
\r\n <-- technically invisible..
GET /path/ HTTP/1.1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22
You could do this by monkeypatching the
requests etc. all use that class to handle the low-level HTTP conversation.
The easiest is to patch the
HTTPConnection._output() method to insert the extra characters before a
HTTP version message:
from httplib import HTTPConnection, _CS_REQ_STARTED orig_output = HTTPConnection._output def add_initial_newline_output(self, s): if (self._HTTPConnection__state == _CS_REQ_STARTED and s.endswith(self._http_vsn_str) and not self._buffer): self._buffer.append('') # will insert extra \r\n orig_output(self, s) HTTPConnection._output = add_initial_newline_output
This will only insert the extra starting empty line when the connection is in the correct state (request started), the line ends with the current HTTP version string, and the buffer is still empty.