簡體   English   中英

POST 多部分/表單數據 postman 與 python 請求

[英]POST multipart/form-data postman vs python request

我想使用 python 請求將多部分/表單數據作為帖子正文發送,但我沒有收到錯誤的請求問題。

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"))

output:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM