The core elements of an HTTP request are the method, url, headers and body.
httpx ahttpx
>>> 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''
>>> req = ahttpx.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''
The following headers have automatic behavior with Requests instances...
Host- AHostheader must always be included on a request. This header is automatically populated from theurl, using theurl.netlocproperty.Content-Length- Requests including a request body must always include either aContent-Lengthheader or aTransfer-Encoding: chunkedheader. This header is automatically populated ifcontentis notNoneand the content is a known size.Transfer-Encoding- Requests automatically include aTransfer-Encoding: chunkedheader ifcontentis notNoneand the content is an unkwown size.Content-Type- Requests automatically include aContent-Typeheader ifcontentis set using the [Content Type] API.
Including binary data directly...
httpx ahttpx
>>> headers = {'Content-Type': 'application/json'}
>>> content = json.dumps(...)
>>> httpx.Request('POST', 'https://echo.encode.io/', content=content)
>>> headers = {'Content-Type': 'application/json'}
>>> content = json.dumps(...)
>>> ahttpx.Request('POST', 'https://echo.encode.io/', content=content)
Including JSON request content...
httpx ahttpx
>>> data = httpx.JSON(...)
>>> httpx.Request('POST', 'https://echo.encode.io/', content=data)
>>> data = ahttpx.JSON(...)
>>> ahttpx.Request('POST', 'https://echo.encode.io/', content=data)
Including form encoded request content...
httpx ahttpx
>>> data = httpx.Form(...)
>>> httpx.Request('PUT', 'https://echo.encode.io/', content=data)
>>> data = ahttpx.Form(...)
>>> ahttpx.Request('PUT', 'https://echo.encode.io/', content=data)
Including multipart file uploads...
httpx ahttpx
>>> 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]>
>>> form = ahttpx.MultiPart(form={...}, files={...})
>>> async with ahttpx.Request('POST', 'https://echo.encode.io/', content=form) as req:
>>> req.headers
{...}
>>> req.stream
<MultiPartStream [0% of ...MB]>
Including direct file uploads...
httpx ahttpx
>>> 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]>
>>> file = ahttpx.File('upload.json')
>>> async with ahttpx.Request('POST', 'https://echo.encode.io/', content=file) as req:
>>> req.headers
{...}
>>> req.stream
<FileStream [0% of ...MB]>
In progress...
httpx ahttpx
>>> data = request.json()
>>> data = await request.json()
...
httpx ahttpx
>>> form = request.form()
>>> form = await request.form()
...
httpx ahttpx
>>> files = request.files()
>>> files = await request.files()