RESTful POST JSON flash not working

I have a problem sending JSON via curl from cmd (Windows7) to Flask RESTful. Here is what I post:

curl.exe -i -H "Content-Type: application/json" \ -H "Accept: application/json" -X POST \ -d '{"Hello":"Karl"}' http://example.net:5000/ 

This leads to bad requests, also I donโ€™t know how to debug this, usually I print information to the console, but it doesnโ€™t work. How do you debug wsgi applications? It seems like a hopeless task ...

This is my simple test application, as shown on the net:

 from flask import Flask, request from flask.ext.restful import Resource, Api app = Flask(__name__) api = Api(app) class Test(Resource): def post(self): #printing request.data works json_data = request.get_json(force=True) # this issues Bad request # request.json also does not work return {} api.add_resource(Test, '/') if __name__ == '__main__': app.run(debug=True) 
+5
json python rest flask curl
source share
3 answers

-d '{"Hello":"Karl"}' does not work from windows, surrounded by single quotes. Use double quotes and it will work for you.

 -d "{\"Hello\":\"Karl\"}" 
+6
source share

I just want to point out that you need to run away regardless of the OS - and regardless of whether you have double quotes around the request data - I saw this post and did not think it was the answer to my question, because I had a double quotes request data and single quotes inside:

This will not work:

 -d "{'Hello': 'Karl'}" 

This will:

 -d "{\"Hello\":\"Karl\"}" 

Again, you need to avoid quotation marks, regardless of the OS (I'm on a Mac), and whether you have single or double quotes

And thanks to Sabuj Hassan for your reply!

0
source share

To add to the previous two answers, you do not need to avoid quotes in all OSs, after that the syntax will work fine on Mac / Linux:

 -d '{"Hello":"Karl"}' 
0
source share

All Articles