[英]Unable to send POST request from postman to API made using Flask
嗨,我是第一次使用python編寫Web API。 而且我對REST的了解有限
我有一個簡單的Flask API,它接受python dict {'pdf':pdf_as_bytes,'filename':string}
以下是我的服務器腳本:
@app.route("/predict", methods=["POST"])
def predict():
data = {"success": False}
if flask.request.method == "POST":
pdf = flask.request.files["pdf"].read()
filename = flask.request.files["filename"].read().decode("utf-8")
assert isinstance(filename, str), "Got {}".format(type(filename))
assert isinstance(pdf, bytes), "Got {}".format(type(pdf))
# further processing happens and returns a json
當我按如下方式編寫python客戶端時,這可以按預期工作:
import requests
import os
ip = "localhost"
port = 8605
url = "http://{}:{}/predict".format(ip,port)
path_to_pdf = "./617339931.pdf"
with open(path_to_pdf, "rb") as f:
pdf = f.read() # pdf is a bytes
# the payload must have the following fields: "pdf": bytes, "filename": string object
payload = {"pdf":pdf,"filename":os.path.basename(path_to_pdf).split(".")[0]}
# post request
result = requests.post(url=url, files=payload).json()
# the resulting json always has a field called as success, which returns True or False accordingly
if result["success"] == True:
print(result["data"].keys())
但是,當我使用郵遞員發送請求時,出現400錯誤! 下面是錯誤的屏幕截圖
我不明白 如何更改服務器代碼,使其與Postman和Python客戶端程序一起使用
我只是做了同樣的事情,我想這是因為您在關鍵和價值中加上了雙引號,請嘗試將其刪除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.