【问题标题】:How to write a python script that does the same thing as this postman request如何编写与此邮递员请求执行相同操作的 python 脚本
【发布时间】:2019-12-15 13:23:04
【问题描述】:

需要调用请求来上传图片,这是一个不只是图片的表单数据

我尝试了几种方法来模仿邮递员图像中的请求,主要是创建数据字典并将其放入发布请求中的不同方法

def uploadimage():

    datain = {
        "file": open('test.png','rb').read(),
        "use": "logoGroup-staging",
        "logoGroup": {"_id": "5d42145db3ef8c0031128763", "name":"sstimagetest2", "longName": "sss",
                           "createdAt": "2019-07-31T22:21:17.680Z", "updatedAt": "2019-07-31T22:21:17.680Z", "__v": 0},
        "imageType": {"_id":"5cfeb4703509f72b616bf7b6","name":"colorhybridnw","__v":0,"createdAt":"2019-06-10T19:50:08.928Z","height":"0","type":"Manual","updatedAt":"2019-06-10T19:50:08.928Z","width":"0"}
    }

    response = requests.post(
        'https://xxxxxxxxxx/api/upload',
        data = datain
    )

在我所有的尝试中,它可以得出 3 个错误类别,错误的 python 数据格式(无法运行),400 错误请求调用,502 错误网关调用

【问题讨论】:

  • 您使用的是什么 api,如果没有看到所需的 POST 格式,将无法为您提供太多帮助。
  • 对不起,第一次创建问题,我刚刚添加了我想要的帖子格式的图像
  • 你也应该包含标题。 res = req.post(url=url, headers=headers, data=datain)
  • 我也尝试过在邮递员中使用/不使用标题,无论哪种方式都可以,当我在 python 中尝试时,无论是否使用标题都不起作用
  • 您可以发布您正在使用的文档的链接吗?

标签: python image post python-requests postman


【解决方案1】:
import requests

files = {'file': open('test.png','rb').read()}
datain = {'all': 'other', 'data': 'not', 'file': 'upload'}

r = requests.post(url, files=files, data=datain)

requests 负责处理多部分表单 POST。

【讨论】:

  • 我试着改成那种方式,新的错误是 for (k, v) in files: ValueError: too many values to unpack (expected 2)
猜你喜欢
  • 1970-01-01
  • 2015-10-26
  • 2021-03-16
  • 2020-12-13
  • 2023-03-09
  • 2020-03-11
  • 2022-06-12
  • 2018-09-15
  • 1970-01-01
相关资源
最近更新 更多