简体   繁体   中英

Unable to send POST request from postman to API made using Flask

Hi I am new to writing web APIs in python. And my understanding of REST is limited

I have a simple Flask API that takes in a 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:

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! 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

我只是做了同样的事情,我想这是因为您在关键和价值中加上了双引号,请尝试将其删除。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM