如何在 python 中发出 aiohttp 发布照片请求?

问题描述 投票:0回答:1

我正在尝试将文件上传到网站,但没有成功。我需要获取文件的 uuid 才能在 vinted.hu 上创建帖子。 cookies 和 headers 都没有问题,只有 request :'(

需要任何帮助!

文件接受按钮

<input data_testid="add-photos-input" name="photos" class="u-hidden" type="file" accept="image/*" multiple="">

发送照片的代码

with open('data/post/213.jpg', 'rb') as file:
    async with session.post('https://www.vinted.hu/api/v2/photos',headers=headers, data={"photos": file}) as response:
        print(await response.text())

结果

{"code":99,"message":"Sorry, there are some errors","message_code":"validation_error","errors":[{"field":"base","value":"param is missing or the value is empty: photo\nDid you mean?  photos"}],"payload":{}}

码一发照片

data = FormData()
data.add_field('photos',open('data/post/213.jpg', 'rb'), filename='213.jpg', content_type='image/*')
async with session.post('https://www.vinted.hu/api/v2/photos',headers=headers, data=data) as response:
    print(await response.text())

结果1

{"code":99,"message":"Sorry, there are some errors","message_code":"validation_error","errors":[{"field":"base","value":"param is missing or the value is empty: photo\nDid you mean?  photos"}],"payload":{}}
python post beautifulsoup python-requests aiohttp
1个回答
0
投票

在请求负载中使用

multipart/form-data
内容类型。喜欢下面的代码

import aiohttp

async with aiohttp.ClientSession() as session:
    headers = {...}  # your headers here
    data = aiohttp.FormData()
    with open('data/post/213.jpg', 'rb') as f:
        data.add_field('photos', f, filename='213.jpg', content_type='image/jpeg')
    async with session.post('https://www.vinted.hu/api/v2/photos', headers=headers, data=data) as response:
        if response.status == 200:
            data = await response.json()
            # do something with the response
        else:
            # handle the error
© www.soinside.com 2019 - 2024. All rights reserved.