[英]How can i send file with aiohttp?
這是我的代碼:
payload = {'text': input_text,
'question_info': '',
'include_intonation': 1,
'stress_version': stress_version,
'include_fluency': 1,
'include_ielts_subscore': 1}
files = [
('user_audio_file', open(saved_file_path, 'rb'))
]
headers = {}
form = aiohttp.FormData()
for key, value in payload.items():
form.add_field(key, value)
form.add_field('user_audio_file', open(saved_file_path, 'rb'))
async with aiohttp.ClientSession() as session:
async with session.post(url,data=form) as response:
response_json = await response.json()
我想將帶有 aiohttp 的文件發送到 URL 但我遇到了這個異常
'Can not serialize value type: <class \'int\'> headers: {} value: 1'
我用這樣的請求庫來做到這一點
response = request(
"POST", url, headers=headers, data=payload, files=files)
response_json = response.json()
但我決定使用 aiohttp 因為它應該是異步的,請幫助我做出這個決定
謝謝
您需要使用data= b'form'
序列化有效負載數據,例如
async with aiohttp.ClientSession() as session:
async with session.post(url,data=b'form') as response:
response_json = await response.json()
默認情況下,會話使用 python 的標准 json 模塊進行序列化。 但是可以使用不同的序列化程序。 ClientSession 接受 json_serialize 參數。 然后您不需要顯式序列化您的有效負載。
import ujson
async with aiohttp.ClientSession(
json_serialize=ujson.dumps) as session:
await session.post(url,data=form) as response:
response_json = await response.json()
....
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.