httpx 请求实例
2022-07-26 11:46 更新
为了最大程度地控制通过网络发送的内容,HTTPX 支持构建显式请求实例:
request = httpx.Request("GET", "https://example.com")
要将实例分派到网络,请创建一个Client实例并使用 Request.send()
:
with httpx.Client() as client:
response = client.send(request)
...
如果需要以默认的参数合并不支持的方式混合client-level
和request-level
选项,可以使用.build_request()
,然后对Request
实例进行任意修改。例如:
headers = {"X-Api-Key": "...", "X-Client-ID": "ABC123"}
with httpx.Client(headers=headers) as client:
request = client.build_request("GET", "https://api.example.com")
print(request.headers["X-Client-ID"]) # "ABC123"
# Don't send the API key for this particular request.
del request.headers["X-Api-Key"]
response = client.send(request)
...
以上内容是否对您有帮助:
更多建议: