[英]Unable to send POST request from postman to API made using Flask
Hi I am new to writing web APIs in python. 嗨,我是第一次使用python编写Web API。 And my understanding of REST is limited
而且我对REST的了解有限
I have a simple Flask API that takes in a python dict {'pdf':pdf_as_bytes, 'filename':string} 我有一个简单的Flask API,它接受python dict {'pdf':pdf_as_bytes,'filename':string}
The below is my server script: 以下是我的服务器脚本:
@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
This works as intended when I write a python client as follows: 当我按如下方式编写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())
But, When I send a request using Postman I get a 400 Error! 但是,当我使用邮递员发送请求时,出现400错误! Below is the screen shot of the error
下面是错误的屏幕截图
I don't understand. 我不明白 How can I change my server code so that it works with Postman and also Python client programs
如何更改服务器代码,使其与Postman和Python客户端程序一起使用
我只是做了同样的事情,我想这是因为您在关键和价值中加上了双引号,请尝试将其删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.