httpx 請求實例

2022-07-26 11:46 更新

為了最大程度地控制通過網(wǎng)絡(luò)發(fā)送的內(nèi)容,HTTPX 支持構(gòu)建顯式請求實例:

request = httpx.Request("GET", "https://example.com")

要將實例分派到網(wǎng)絡(luò),請創(chuàng)建一個Client實例并使用? Request.send()?:

with httpx.Client() as client:
    response = client.send(request)
    ...

如果需要以默認的參數(shù)合并不支持的方式混合?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)
    ...


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號