【问题标题】:Curl -d : Contents of the file must be URL Encodedcurl -d :文件的内容必须是 URL 编码的
【发布时间】:2015-05-09 23:34:58
【问题描述】:

我有一个使用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 文件。我不完全知道应该在这里使用哪种编码。请帮忙!!

【问题讨论】:

    标签: python json curl flask


    【解决方案1】:

    当我尝试你的代码时,我得到了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'}%
    

    【讨论】:

    • json 结尾的 % 是多少?
    • 那是 zsh 的输出不以换行符结尾的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多