您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – 通过Curl向Flask发送JSON-Request [复制]

5b51 2022/1/14 8:23:09 python 字数 2296 阅读 584 来源 www.jb51.cc/python

参见英文答案 > How to get POSTed json in Flask?????????????????????????????????????4个 >???????????? How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?

概述

from flask import Flask,request

app = Flask(__name__)

@app.route('/post',methods=['POST'])
def post_route():
    if request.method == 'POST':

        data = request.get_json()

        print('Data Received: "{data}"'.format(data=data))
        return "Request Processed.\n"

app.run()

这是我尝试从命令行发送的curl请求:@H_502_7@

curl localhost:5000/post -d '{"foo": "bar"}'

但仍然打印出“收到的数据:”无“”.所以,它无法识别我传递的JSON.@H_502_7@

在这种情况下是否有必要指定json格式?@H_502_7@

[..] function will return None if the mimetype is not application/json but this can be overridden by the force parameter.@H_502_7@

因此,要么将传入请求的mimetype指定为application / json:@H_502_7@

curl localhost:5000/post -d '{"foo": "bar"}' -H 'Content-Type: application/json'

或使用force = True强制进行JSON解码:@H_502_7@

data = request.get_json(force=True)

如果在Windows上运行此命令(cmd.exe,而不是PowerShell),则还需要更改JSON数据的引用,从单引号到双引号:@H_502_7@

curl localhost:5000/post -d "{\"foo\": \"bar\"}" -H 'Content-Type: application/json'

总结

以上是编程之家为你收集整理的python – 通过Curl向Flask发送JSON-Request [复制]全部内容,希望文章能够帮你解决python – 通过Curl向Flask发送JSON-Request [复制]所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶