[英]Python multipart/form-data Post Request
So I've been trying to send a multipart/form-data request, unsuccessfully.所以我一直在尝试发送一个 multipart/form-data 请求,但没有成功。 At the current moment I get the error back that the Required part of the request is missing.
目前,我收到错误消息,指出缺少请求的必需部分。
{'message': "Required request part 'registrationMetadata' is not present", 'httpStatus': '500 INTERNAL_SERVER_ERROR'}
I know it's unwise to specify the headers in a request as Requests takes care of that.我知道在请求中指定标头是不明智的,因为 Requests 会负责。 But if I don't I get the following error:
但如果我不这样做,我会收到以下错误:
'''{'message': "Content type '' not supported", 'httpStatus': '500 INTERNAL_SERVER_ERROR'}''' '''{'message': "Content type '' not supported", 'httpStatus': '500 INTERNAL_SERVER_ERROR'}'''
The disable warning and verify=false are unimportant in the case of this application as it is not reachable for the outside world.在此应用程序的情况下,禁用警告和 verify=false 并不重要,因为外部世界无法访问它。
This is my python script:这是我的python脚本:
import requests
from requests_toolbelt.utils import dump
import json
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
url = "<ValidUrl>"
data = {"registrationMetadata": '{"firstName":"<name>","lastName":"<lastName>","mobileNumber":"<mobilenumber>","serialNumber":"1234","country":"BE","signingProfile":"sms","externalId":"<externalId>","email": "<email>","language": "nl"}'}
headers = {'Content-Type': 'multipart/form-data; boundary=ebf9f03029db4c2799ae16b5428b06bd'}
auth = ('<username>', '<password>')
session = requests.session()
response = session.post(url, data=str(data), auth=auth, verify=False)
req_dump = dump.dump_all(response)
print(req_dump.decode('utf-8'))
print(response.request.body)
print(response.json())
I'll add the headers of the request:我将添加请求的标头:
< POST <valid url> HTTP/1.1
< Host: <valid host>
< User-Agent: python-requests/2.22.0
< Accept-Encoding: gzip, deflate
< Accept: */*
< Connection: keep-alive
< Content-Type: multipart/form-data; boundary=ebf9f03029db4c2799ae16b5428b06bd
< Content-Length: 237
< Authorization: Basic <valid auth key>==
The request works in our java framework and postman.该请求适用于我们的 Java 框架和邮递员。 So I'm at wits end.
所以我不知所措。 In the meantime I've searched for an applicable solution.
与此同时,我一直在寻找适用的解决方案。 Including the MultipartEncoder.
包括 MultipartEncoder。 Neither seems to have worked.
两者似乎都没有奏效。 I'm assuming the difficulty lies in the nested values of the formData.
我假设困难在于 formData 的嵌套值。 And that I'm missing something obvious.
而且我错过了一些明显的东西。
This is very specific to this endpoint and registrationMetadata is not part of the standard.这是非常特定于此端点的,并且 registrationMetadata 不是标准的一部分。 So my suggestion below may have an impact or it may not, it really depends on the server-side which without seeing we can't provide much assistance at all I'm afraid.
所以我下面的建议可能会产生影响,也可能不会,这真的取决于服务器端,如果没有看到我们恐怕根本无法提供太多帮助。
However, it would appear in your registrationMetadata data field you are including single quotes around your inner-dict as part of the string.但是,它会出现在您的 registrationMetadata 数据字段中,您在内部字典周围包含单引号作为字符串的一部分。 What I think you might be after is this:
我认为你可能追求的是:
data = {
"registrationMetadata": {"firstName":"<name>","lastName":"<lastName>","mobileNumber":"<mobilenumber>","serialNumber":"1234","country":"BE","signingProfile":"sms","externalId":"<externalId>","email": "<email>","language": "nl"},
}
rather than what you're doing, which is this:而不是你在做什么,这是:
data = {
"registrationMetadata": '{"firstName":"<name>","lastName":"<lastName>","mobileNumber":"<mobilenumber>","serialNumber":"1234","country":"BE","signingProfile":"sms","externalId":"<externalId>","email": "<email>","language": "nl"}',
}
You should also use the 'json' library instead of casting to a string type in your request.您还应该使用 'json' 库,而不是在您的请求中转换为字符串类型。 Just changing it to a string might not be enough.
仅将其更改为字符串可能还不够。
session = requests.session()
response = session.post(url, data=json.dumps(data), auth=auth, verify=False)
EDIT: What I find really helps eliminate this sort of issue is to define my dict's across multiple lines, example below:编辑:我发现真正有助于消除此类问题的是跨多行定义我的字典,示例如下:
data = {
"registrationMetadata": {
"firstName": "<name>",
"lastName": "<lastName>",
"mobileNumber": "<mobilenumber>",
"serialNumber": "1234",
"country": "BE",
"signingProfile": "sms",
"externalId": "<externalId>",
"email": "<email>",
"language": "nl",
},
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.