You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python的aiohttp向Vinted.hu上传图片并获取文件UUID?

Vinted.hu 图片上传接口请求错误排查与解决

尝试调用Vinted.hu的/api/v2/photos接口上传图片以获取文件UUID用于发布内容,但请求始终返回验证错误,提示缺少photo参数(同时提示是否应为photos)。已确认Cookie与Header配置无误,问题出在请求构造环节。

页面文件选择按钮代码

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

已尝试的请求代码及返回结果

方式1:直接用字典传递文件

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":{}}

方式2:使用FormData传递文件

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())

返回结果:

{"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":{}}

问题原因与解决方法

接口提示信息存在误导性:页面表单的photos字段是前端封装的名称,实际接口要求单文件上传时字段名为photo,多文件上传时字段名为photo[]。

修正后的单文件上传代码

data = FormData()
# 字段名改为`photo`,并指定具体的图片MIME类型
data.add_field('photo', open('data/post/213.jpg', 'rb'), filename='213.jpg', content_type='image/jpeg')
async with session.post('https://www.vinted.hu/api/v2/photos', headers=headers, data=data) as response:
    print(await response.text())

多文件上传代码(如需批量上传)

data = FormData()
# 多次添加`photo[]`字段实现多文件上传
data.add_field('photo[]', open('data/post/213.jpg', 'rb'), filename='213.jpg', content_type='image/jpeg')
data.add_field('photo[]', open('data/post/456.jpg', 'rb'), filename='456.jpg', content_type='image/png')
async with session.post('https://www.vinted.hu/api/v2/photos', headers=headers, data=data) as response:
    print(await response.text())

额外注意事项

  1. 避免使用通用的image/*作为content_type,改用具体的图片类型(如image/jpeg、image/png),接口会严格校验该参数。
  2. 使用FormData时,不要手动设置Content-Type: multipart/form-data请求头,aiohttp会自动生成包含正确边界值的请求头,手动设置会导致校验失败。

内容的提问来源于stack exchange,提问作者WorkAholic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 18:32:07