简体   繁体   English

Python HTTP Post,其中包含从Postman生成的上传文件和标头

[英]Python HTTP Post with upload file and headers generated from Postman

I'm using Python 2.7 . 我正在使用Python 2.7

I want to make a HTTP POST using requests , where I upload a file and a key that must go in the HTTP Headers . 我想使用requests进行HTTP POST ,在该requests中上传文件和必须放在HTTP Headers的密钥。

For that I've used the application Postman , where it works really fine. 为此,我使用了Postman应用程序,它在这里确实可以正常工作。 邮递员的例子

On Postman I've added only the necessary header, which is a Authorization with some key. Postman我仅添加了必要的标头,即带有某些密钥的Authorization On the body , Ive choosen form-data and then the key is an input_image , and they the image itself. body ,我选择了form-data ,然后键是一个input_image ,它们就是图像本身。

Now I want to replicate this into Python2.7 , so I've chose to see the Python code on Postman , which was this one: 现在,我想将其复制到Python2.7 ,所以我选择在Postman上查看Python代码,它就是这样的代码:

import requests

url = "https://foo.com/bar/stuff"

payload = "------WebKitFormBoundary7MA4YDxkTrZu1gW\r\nContent-Disposition: form-data; name=\"input_image\"; filename=\"C:\\Test\\projs\\Supermarket\\doritos.jpeg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YDxkTrZu1gW--"
headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YDxkTrZu1gW",
    'Authorization': "myAuthorizationKey",
    'Cache-Control': "no-cache",
    'Postman-Token': "0efwd6e8-051c-4ed5-8d6f-7b1bd135f4d5"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)

This simply doesn't work. 这根本行不通。 It has the same behaviour as if I didn't send any image using Postman . 它具有与我没有使用Postman发送任何图像相同的行为。 It looks like the payload string is not being send correctly. 看来负载字符串未正确发送。

Question: 题:

What is wrong with this Postman auto-generation code in order to send a HTTP POST with image upload and with header at the same time in Python ? 为了在Python同时发送带有image uploadheaderHTTP POST ,此Postman自动生成代码有什么问题?

I think Postman is doing some logic we are not really aware of. 我认为Postman正在做一些我们并未真正意识到的逻辑。 But the package requests provide a way to upload images. 但是打包requests提供了一种上传图像的方法。

files = {'media': open('my_image.jpg', 'rb')}
r = requests.post(url, files=files, headers=hearders)

According to the server you are sending the image to, the parameters name, this code might need to be slightly changed. 根据要将图像发送到的服务器(参数名称),此代码可能需要稍作更改。

the only trick works here is your code should be same as you post request in postman, no extra headers need to be added , your post request should look like the same as it is in postman. 唯一有用的技巧是您的代码应与邮递员中的发布请求相同,无需添加额外的标头,您的邮寄请求应与在邮递员中看起来相同。 I could do this by changing my file to an image file and then posting it in my post request. 我可以通过将文件更改为图像文件,然后将其发布到我的发布请求中来做到这一点。

    with open('grass-small.png', 'rb') as imageFile:
        imageStr = base64.urlsafe_b64encode(imageFile.read())
    files = {'document': ('grass-small.png', imageStr ), 'document_type':(None,'grass')}

This worked for me 这对我有用

import requests

url = 'http://iizuka.cs.tsukuba.ac.jp/projects/colorization/web/'
files = {'file': ("my_img_path/myImage.jpeg", open('my_img_path/myImage.jpeg', 'rb'),'image/jpg')}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTTP Post请求以及上传文件和标头可在Postman中使用,但不适用于python - HTTP Post request with upload file and headers works from Postman but not working in python 使用 python 中的请求模块上传文件不起作用(邮递员生成的代码) - Upload file using requests module in python is not working (the code generated by Postman) 使用 python http post 从内存上传二进制数据或文件 - upload binary data or file from memory using python http post python:解析HTTP POST请求w /文件上传和其他参数 - python: parse HTTP POST request w/file upload and additional params 文件上传适用于curl和postman,但不适用于python请求 - file upload works with curl and postman but not python requests 使用python上传生成的大文件 - Upload generated big file with python 发布到 API 适用于 Postman 但生成的 Python 代码不起作用 - Post to API works on Postman but the generated Python code does not 简单的Python服务器程序通过Postman的http POST接收图像? - Simple Python server program to receive an image via http POST from Postman? 使用 Selenium cookies 和 postman 在 ZA7F5F35426B92741139231B56382 中生成 POST 片段 - Use Selenium cookies with postman generated POST snippet in Python Python - 从生成的响应上传文件到 S3 - Python - Upload file to S3 from generated response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM