简体   繁体   English

无法通过python请求模块发送`multipart / form-data`请求

[英]Unable to send `multipart/form-data` request with python requests module

I have Java spring server that requires the Content-Type of the requests being sent to the server to be multipart/form-data . 我有Java spring服务器,它要求发送到服务器的请求的Content-Typemultipart/form-data

I can correctly send requests to the server with postman: 我可以使用邮递员将请求正确发送到服务器:

在此处输入图片说明

在此处输入图片说明

However, I got The current request is not a multipart request error when trying to send the request with requests module in python3. 但是,我尝试在python3中使用requests模块发送请求时The current request is not a multipart request错误。

My python code is: 我的python代码是:

import requests

headers = {
  'Authorization': 'Bearer auth_token'
}

data = {
  'myKey': 'myValue'
}

response = requests.post('http://127.0.0.1:8080/apiUrl', data=data, headers=headers)
print(response.text)

If I add 'Content-Type': 'multipart/form-data' to the header of the request, the error message then becomes Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found 如果我将'Content-Type': 'multipart/form-data'到请求的标头,则错误消息将变为Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found . Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found

How can I make the same request as postman sends with python? 如何发出与邮递员使用python发送的相同请求?

requests 's author thinks this situation is not pythonic, so requests doesn't support this usage natively. requests的作者认为这种情况不是pythonic的,因此requests本身不支持这种用法。

You need to use requests_toolbelt which is an extension maintained by members of the requests core development team, doc , example: 您需要使用requests_toolbelt ,它是由请求核心开发团队doc的成员维护的扩展,例如:

import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder

m = MultipartEncoder(
    fields={'field0': 'value', 'field1': 'value',
            'field2': ('filename', open('file.py', 'rb'), 'text/plain')}
    )

r = requests.post('http://httpbin.org/post', data=m,
                  headers={'Content-Type': m.content_type})

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

相关问题 骆驼发送多部分/表单数据请求 - Camel send multipart/form-data request Java 9 HttpClient 发送多部分/表单数据请求 - Java 9 HttpClient send a multipart/form-data request 码头8多部分/表格数据请求 - jetty 8 multipart/form-data requests 在Spring 5.0.1中无法处理多部分/报表数据。 它仅在请求中检测到多部分/表单数据 - Unable to process multipart/report data in Spring 5.0.1. It is only detecting multipart/form-data in the request 无法发送多部分/表单数据 - impossible to send multipart/form-data 使用 ByteArray 请求 MultiPart/Form-Data - Request MultiPart/Form-Data with ByteArray 使用 axios POST 请求将 JSON 数据作为多部分/表单数据发送 - Send JSON data as multipart/form-data using axios POST Request 如何在 postman 多部分/表单数据发布请求中将应用程序/json 数据与文件一起发送? - How to send application/json data along with file in postman multipart/form-data post request? 为什么我不能向Microsoft OneNote发送multipart / form-data请求? - Why can't I send a multipart/form-data request to Microsoft OneNote? 如何在邮递员的同一请求中发送多部分/表单数据和嵌套的json? - How to send multipart/form-data and nested json in the same request in postman?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM