Created
October 18, 2018 20:37
-
-
Save pelid/9ec4cdb9fbcbe1154e6ee1c254e5b09a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
req = requests.Request('POST', 'http://stackoverflow.com' , headers={'X-Custom': 'Test'}, data={ | |
'a': 1, | |
'b': 2, | |
}) | |
# TODO допилить напильником | |
# GET /wiki/HTTP HTTP/1.0 | |
# Host: ru.wikipedia.org | |
REQUEST_TMPL = """{req.method} {req.url} | |
{headers_str} | |
{req.body}""" | |
def serialize_http_request(request): | |
""" | |
At this point it is completely built and ready | |
to be fired; it is "prepared". | |
However pay attention at the formatting used in | |
this function because it is programmed to be pretty | |
printed and may differ from the actual request. | |
""" | |
prepared_request = request.prepare() | |
headers_str = '\n'.join('{}: {}'.format(k, v) for k, v in req.headers.items()) | |
return REQUEST_TMPL.format(req=prepared_request, headers_str=headers_str) | |
print(serialize_http_request(req)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment