Requests

The core elements of an HTTP request are the method, url, headers and body.

httpx
>>> req = httpx.Request('GET', 'https://www.example.com/')
>>> req
<Request [GET 'https://www.example.com/']>
>>> req.method
'GET'
>>> req.url
<URL 'https://www.example.com/'>
>>> req.headers
<Headers {'Host': 'www.example.com'}>
>>> req.body()
b''

Working with the request headers

The following headers have automatic behavior with Requests instances...

Working with the request body

Including binary data directly...

httpx
>>> headers = {'Content-Type': 'application/json'}
>>> content = json.dumps(...)
>>> httpx.Request('POST', 'https://echo.encode.io/', content=content)

Working with content types

Including JSON request content...

httpx
>>> data = httpx.JSON(...)
>>> httpx.Request('POST', 'https://echo.encode.io/', content=data)

Including form encoded request content...

httpx
>>> data = httpx.Form(...)
>>> httpx.Request('PUT', 'https://echo.encode.io/', content=data)

Including multipart file uploads...

httpx
>>> form = httpx.MultiPart(form={...}, files={...})
>>> with httpx.Request('POST', 'https://echo.encode.io/', content=form) as req:
>>>     req.headers
{...}
>>>     req.stream
<MultiPartStream [0% of ...MB]>

Including direct file uploads...

httpx
>>> file = httpx.File('upload.json')
>>> with httpx.Request('POST', 'https://echo.encode.io/', content=file) as req:
>>>     req.headers
{...}
>>>     req.stream
<FileStream [0% of ...MB]>

Accessing request content

In progress...

httpx
>>> data = request.json()

...

httpx
>>> form = request.form()

...

httpx
>>> files = request.files()

Servers Responses