pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/encode/httpnext/blob/dev/docs/requests.md

rel="stylesheet" href="https://github.githubassets.com/assets/global-a40b6ece39d70d4a.css" /> httpnext/docs/requests.md at dev · encode/httpnext · GitHub
Skip to content

Latest commit

 

History

History
178 lines (134 loc) · 5.08 KB

File metadata and controls

178 lines (134 loc) · 5.08 KB

Requests

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''

Working with the request headers

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

  • Host - A Host header must always be included on a request. This header is automatically populated from the url, using the url.netloc property.
  • Content-Length - Requests including a request body must always include either a Content-Length header or a Transfer-Encoding: chunked header. This header is automatically populated if content is not None and the content is a known size.
  • Transfer-Encoding - Requests automatically include a Transfer-Encoding: chunked header if content is not None and the content is an unkwown size.
  • Content-Type - Requests automatically include a Content-Type header if content is set using the [Content Type] API.

Working with the request body

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)

Working with content types

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]>

Accessing request content

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()

Servers Responses  

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy