為了最大程度地控制通過網(wǎng)絡(luò)發(fā)送的內(nèi)容,HTTPX 支持構(gòu)建顯式請求實(shí)例:
request = httpx.Request("GET", "https://example.com")
要將實(shí)例分派到網(wǎng)絡(luò),請創(chuàng)建一個(gè)Client實(shí)例并使用? Request.send()
?:
with httpx.Client() as client:
response = client.send(request)
...
如果需要以默認(rèn)的參數(shù)合并不支持的方式混合?client-level
?和?request-level
?選項(xiàng),可以使用?.build_request()
?,然后對?Request
?實(shí)例進(jìn)行任意修改。例如:
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)
...
更多建議: