python 如何通过POST将JSON正确发送到flask Web服务?

68de4m5k  于 2023-03-11  发布在  Python
关注(0)|答案(1)|浏览(141)

我正在尝试将以下JSON数据发送到我的flask服务
{'filesIds': ['0698Y00000Rb5vuQAB', '0698Y00000Rb6GsQAJ']}
但得到的是有线响应:开始是200个,然后是400个错误请求:

Mar 10 02:41:40 PM  127.0.0.1 - - [10/Mar/2023:12:41:40 +0000] "POST /parsed-invoice HTTP/1.1" 200 3033 "-" "SFDC-Callout/57.0"
Mar 10 02:41:41 PM  127.0.0.1 - - [10/Mar/2023:12:41:41 +0000] "POST /parsed-invoice HTTP/1.1" 400 167 "-" "SFDC-Callout/57.0"

在 flask 服务中,我可以得到数据tho.

app.route('/parsed-invoice', methods=['GET', 'POST'])
def parse_invoice():
    files_ids_jsonified = request.get_json()
    print(f"FILE IDS JSON  {files_ids_jsonified} TYPE {type(files_ids_jsonified)}")

输出:FILE IDS JSON {'filesIds': ['0698Y00000Rb5vuQAB', '0698Y00000Rb6GsQAJ']} TYPE <class 'dict'>
我已尝试检查方法是否为post

app.route('/parsed-invoice', methods=['GET', 'POST'])
def parse_invoice():
    if request.method == 'POST':
        files_ids_jsonified = request.get_json()
        print(f"FILE IDS JSON  {files_ids_jsonified} TYPE {type(files_ids_jsonified)}")

但是仍然有这个问题。我也试过get_json(force=True)-它也没有帮助。我也试过使用双引号,它没有工作。
我到底做错了什么?

tcbh2hod

tcbh2hod1#

你已经收到了json。下一步你需要通过它们的键来获取filesIds:

@app.route('/parsed-invoice', methods=['GET', 'POST'])
def parse_invoice():
    if request.method == 'POST':
        data = request.get_json()
        files_ids = data.get('filesIds', [])

相关问题