Reputation: 5148
I have an app running in Python
using Flask
.
The endpoint of the API
looks like this:
@app.route('/postIt', methods =['POST'])
def postReview():
#print flask.request
if flask.request.method == 'POST':
posts = flask.request.get_json()
print posts
return str(posts)
I am trying to send it request using CURL
:
curl http://127.0.0.1:5000/postIt -d @post.json -H "Content-Type: application/json"
where post.json
looks like this:
{"post1":"3", "post2": "2", "post3":"3", "post4":"4" "post5": "5"}
This is not working well. Output on server side prints nothing implying that it is unable to get the json file that I am sending.
I looked into -d flag of CURL
and found this thing:
The contents of the file must already be URL-encoded.
So I am guessing there must be encoding issues with my post.json
file. I don't exactly know which encoding should be used here. Please help!!
Upvotes: 0
Views: 151
Reputation: 16770
When I tried your code, I got 400 Bad Request
:
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
Then I found that your json file is actually not valid. There is a missing ,
between post4
and post5
. After fixing it, I got the correct response:
{u'post5': u'5', u'post4': u'4', u'post3': u'3', u'post2': u'2', u'post1': u'3'}%
Upvotes: 1