Hello, I am writing a code for a long time I wanted to make a small application using the request.

from flask import Flask, request import json app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' @app.route('/a') def a(): page = request.args.get('page', default = 0, type = int) filter = request.args.get('filter', default = '*', type = str) RE = 'page: ' + str(page) + '\n' + 'filter: ' + str(filter) return RE @app.route('/add', methods=['POST']) def add(): f = request.json() return f if __name__ == '__main__': app.run(host='0.0.0.0', port=5000) 

I make a request through curl:

 curl -H -X POST "aplication/json" {"Name": "Sasha"} "http://127.0.0.1:5000/add" 

Conclusion:

 curl: (6) Could not resolve host: POST curl: (6) Could not resolve host: aplication curl: (3) [globbing] unmatched brace in column 1 curl: (3) [globbing] unmatched close brace/bracket in column 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <title>405 Method Not Allowed</title> <h1>Method Not Allowed</h1> <p>The method is not allowed for the requested URL.</p> 

Tell me what is the error ??

Rewrote the line as requested:

 $curl -X POST -H "aplication/json" -d '{"Name": "Sasha"}' "http://127.0.0.1:5000/add" <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <title>500 Internal Server Error</title> <h1>Internal Server Error</h1> <p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p> 

Server Error:

 'The view function did not return a valid response. The' TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement. 127.0.0.1 - - [06/Jul/2018 18:39:38] "POST /add HTTP/1.1" 500 - 

Changed two lines:

 f = request.json() return f 

On:

 f = request.get_json() return "Add the json request." 

It all worked!)

  • one
    You have written something in curl, read his certificate and make the correct command - andreymal
  • Rewrote, but still only gives 500 error. - Max Adamchuk
  • @MaxAdamchuk and now you need to look at the server logs, it says what exactly is the error. - Sergey Gornostaev
  • Thank you, I've already seen and corrected) - Max Adamchuk

1 answer 1

You have several errors in the code and how it is addressed in the curl command.

Due to an invalid curl command, the Content-Type header is not set in application / json, so request.json returns None. That is, you should get a TypeError: 'NoneType' object is not callable with your code.

Even if you fix the curl command, it will only replace the error with: TypeError: 'dict' object is not callable . Without fixing the curl command, request.get_json() also does not work ( None is returned by default).

Here is an example of a simple echo application that returns json text passed to it:

 from flask import Flask, jsonify, request app = Flask(__name__) @app.route('/echo', methods=['POST']) def echo(): return jsonify(request.get_json(force=True)) 

To run, for a test, an application:

 $ FLASK_APP=app.py flask run -h localhost -p 5001 

Request example:

 $ curl --request POST --header 'Content-Type: application/json' --data '{"key": "value"}' 'http://127.0.0.1:5001/echo' { "key": "value" } 

Such requests are convenient with the help of https://httpie.org/ to do:

 $ http --body :5001/echo key=value { "key": "value" } 

If you make a request with an incorrect Content-Type header, then thanks to force=True , this 400 Bad Request HTTP application will return the status (indication of an erroneous request).