Curl d:文件的内容必须是URL编码的

2024-04-20 01:42:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个应用程序运行在Python中,使用Flask。你知道吗

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文件。我不知道这里应该用哪种编码。请帮忙!!你知道吗


Tags: 文件of程序运行apijsonflaskrequestcurl
1条回答
网友
1楼 · 发布于 2024-04-20 01:42:53

当我尝试你的代码时,我得到了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文件实际上是无效的。在post4post5之间缺少,。修好后,我得到了正确的回答:

{u'post5': u'5', u'post4': u'4', u'post3': u'3', u'post2': u'2', u'post1': u'3'}%

相关问题 更多 >