flask can't get post data from postman or web page,but work in python request

rock king

I use flask as API server. I test it with python it's ok!

class ModelLoader(MethodView):
    def __init__(self):
        pass

    def post(self):
        content = request.get_json()
        request
        X_input = content['X_input']
        X_in = str(X_input)
        pred_val,posib = predictor.predict(X_input=X_in)
        pred_val = pred_val.tolist()
        posib = str(round(posib,4)*100)
        #posib = posib.tolist()
        return jsonify({'pred_val': pred_val,'confident':posib})

    def initialize_models(json_path, weights_path, normalized_x, normalized_y):
        global predictor
        predictor = Predictor(json_path, weights_path, normalized_x, normalized_y)
        predictor.compile_model(loss='categorical_crossentropy', optimizer='adam')


    def run(host='0.0.0.0', port=19865):
        """Run a WSGI server using gevent."""
        app.add_url_rule('/predict', 
        view_func=ModelLoader.as_view('predict'))
        print('running server http://{0}'.format(host + ':' + str(port)))
        WSGIServer((host, port), app).serve_forever()

test code

import requests
def get_predictions(X_input):
    """Get predictions from a rest backend for your input."""
    print("Requesting prediction for XOR with {0}".format(X_input))
    r = requests.post("http://yansheng.wang:19865/predict", json={'input':  X_input})
print(r.status_code, r.reason)
resp = r.json()
prediction = resp['pred_val']
confident = resp['confident']
print("XOR of input: {0} is {1},on confident of {2} ".format(X_input, prediction,confident))

if __name__ == '__main__':

    X_inputs = ["885863343916543724"]

    for x_input in X_inputs:
        get_predictions(x_input)

when I run it I can't get post data from postman or web page!

the server console output difference is like below,what's wrong?:

45.76.182.34 - - [2017-07-29 12:11:55] "POST /predict HTTP/1.1" 200 162 0.044396
45.76.182.34 - - [2017-07-29 12:12:03] "POST /predict?input=3423432432424233 HTTP/1.1" 400 303 0.001046
Vikash Singh

when you are testing the code with your test code you are passing the X_input value in json body.

# part of request
json={'input':  X_input}

# I would suggest make the key consistent

# inside post menthod
content = request.get_json()
request
X_input = content['X_input']
X_in = str(X_input)

But when you are sending it from web/postman you are sending it in url params.

"POST /predict?input=3423432432424233 HTTP/1.1" 400

Hence it's failing. Send this data from postman in json body. And it should work.

example:

postman post call example with json body

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can't authenticate a POST/GET request method from Postman, in Spring-boot with self-signed https

flask returns 400 on post request from postman

POST request using Python flask - postman

Can't get the data using importXML from Dynamic Web Page?

Can't post data with postman

Pass a date as a get request on postman however i can't find data from database in laravel

Can't get the body from a JSON WEB API request in python

Can't get request body with resify using Postman to send data

Can't get POST data using NodeJS/ExpressJS and Postman

Python - Flask - Stop page from re-submitting a post request

Request with python doesn't work but postman work

Can't Post data from postman to database with express

Can't get data back to the web page

Get parameters in http post request from postman

Can't get Resource from POST/PUT request (.NET FHIR client, web api 2)

Is there a way for postman to read the response data from a GET request and then use an IF THEN statement to run a POST request?

Post request can't fetch response from targeted page

Java Proxy - can't exchange data from HTTP GET/POST request properly

Get post data from ajax post request in python file

Can't make Curl POST request from web app

Django backend on port 8083 can't parse JQuery AJAX CORS POST request served from gulp web page on port 8081

Android Retrofit POST request doesn't work but in Postman it works

HTTP POST request works in Postman but doesn't work in code

How can I get the data from an API using Python Request Post?

Ajax post request can't get data with express router

Get Request work in postman but doesn't work in browser

Can't get my api to work in Postman, POST returns 404 . its a vue frontend and express backend

POST Request Python Web Scraping: Get URLs from Tag

Can't get post parameter in Golang from ajax request