我使用Flask
在Python
中运行的应用程序。
API
的端点看起来像:
@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)
我试图使用CURL
发送请求:
curl http://127.0.0.1:5000/postIt -d @post.json -H "Content-Type: application/json"
post.json
看起来像这样:
{"post1":"3", "post2": "2", "post3":"3", "post4":"4" "post5": "5"}
这不是很好。服务器端的输出并不意味着它无法获取我要发送的JSON文件。
我研究了-d flag of CURL
并找到了这个东西:
The contents of the file must already be URL-encoded.
所以我猜必须有encoding issues with my post.json
文件。我不知道这里应该使用哪种编码。请帮助!
当我尝试您的代码时,我得到了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>
然后,我发现您的JSON文件实际上无效。post4
和post5
之间缺少,
。修复后,我得到了正确的响应:
{u'post5': u'5', u'post4': u'4', u'post3': u'3', u'post2': u'2', u'post1': u'3'}%