POST multipart/form-data 邮递员 vs python 请求
POST multipart/form-data postman vs python request
我想使用 python 请求将 multipart/form-data 作为 post 正文发送,但我没有遇到错误的请求问题。
import requests
headers = {"Content-Type": "multipart/form-data"}
data = {
"@context": "http://semantro.com/", "@type": "KiranaSearch", "actionName": "listCategoryProducts",
"pageLimit": {"@context": "http://semantro.com/", "@type": "PageProperty", "start": 0, "end": 24},
"data": {"@context": "http://semantro.com/", "@type": "KiranaCategory",
"identifier": "c5394d1d5c6c4cb8-adc77dd996876dba"}
}
response = requests.post('https://merokirana.com/semantro-web-interface/query',
data=data, headers=headers)
print(response.text)
回应
{
"statusTitle" : "ServiceUnsuccessful",
"statusMessage" : "Invalid type of data received. The request should have multipart query data.",
"@context" : "http://semantro.com",
"@type" : "RemoteServiceStatus"
}
但是我可以使用 postman 相同的表单数据来检索所需的数据。
import http.client
import mimetypes
from codecs import encode
conn = http.client.HTTPSConnection("merokirana.com")
dataList = []
boundary = 'wL36Yn8afVp8Ag7AmP8qZ0SA4n1v9T'
dataList.append(encode('--' + boundary))
dataList.append(encode('Content-Disposition: form-data; name=data;'))
dataList.append(encode('Content-Type: {}'.format('text/plain')))
dataList.append(encode(''))
dataList.append(encode("{ \"@context\": \"http://semantro.com/\", \"@type\": \"KiranaSearch\", \"actionName\": \"listCategoryProducts\",\"pageLimit\": {\"@context\": \"http://semantro.com/\", \"@type\": \"PageProperty\", \"start\": 0, \"end\": 24}, \"data\": {\"@context\": \"http://semantro.com/\", \"@type\": \"KiranaCategory\",\"identifier\": \"c5394d1d5c6c4cb8-adc77dd996876dba\"}}"))
dataList.append(encode('--'+boundary+'--'))
dataList.append(encode(''))
body = b'\r\n'.join(dataList)
payload = body
headers = {
'Content-type': 'multipart/form-data; boundary={}'.format(boundary)
}
conn.request("POST", "/semantro-web-interface/query", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
输出:
我想使用 python 请求将 multipart/form-data 作为 post 正文发送,但我没有遇到错误的请求问题。
import requests
headers = {"Content-Type": "multipart/form-data"}
data = {
"@context": "http://semantro.com/", "@type": "KiranaSearch", "actionName": "listCategoryProducts",
"pageLimit": {"@context": "http://semantro.com/", "@type": "PageProperty", "start": 0, "end": 24},
"data": {"@context": "http://semantro.com/", "@type": "KiranaCategory",
"identifier": "c5394d1d5c6c4cb8-adc77dd996876dba"}
}
response = requests.post('https://merokirana.com/semantro-web-interface/query',
data=data, headers=headers)
print(response.text)
回应
{
"statusTitle" : "ServiceUnsuccessful",
"statusMessage" : "Invalid type of data received. The request should have multipart query data.",
"@context" : "http://semantro.com",
"@type" : "RemoteServiceStatus"
}
但是我可以使用 postman 相同的表单数据来检索所需的数据。
import http.client
import mimetypes
from codecs import encode
conn = http.client.HTTPSConnection("merokirana.com")
dataList = []
boundary = 'wL36Yn8afVp8Ag7AmP8qZ0SA4n1v9T'
dataList.append(encode('--' + boundary))
dataList.append(encode('Content-Disposition: form-data; name=data;'))
dataList.append(encode('Content-Type: {}'.format('text/plain')))
dataList.append(encode(''))
dataList.append(encode("{ \"@context\": \"http://semantro.com/\", \"@type\": \"KiranaSearch\", \"actionName\": \"listCategoryProducts\",\"pageLimit\": {\"@context\": \"http://semantro.com/\", \"@type\": \"PageProperty\", \"start\": 0, \"end\": 24}, \"data\": {\"@context\": \"http://semantro.com/\", \"@type\": \"KiranaCategory\",\"identifier\": \"c5394d1d5c6c4cb8-adc77dd996876dba\"}}"))
dataList.append(encode('--'+boundary+'--'))
dataList.append(encode(''))
body = b'\r\n'.join(dataList)
payload = body
headers = {
'Content-type': 'multipart/form-data; boundary={}'.format(boundary)
}
conn.request("POST", "/semantro-web-interface/query", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
输出: