将JSON发布到Flask将导致400错误的请求错误 [英] Posting JSON to Flask results in 400 Bad Request error

查看:588
本文介绍了将JSON发布到Flask将导致400错误的请求错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一篇文章讨论了如何从MongoDB获取参数和列的json,但是当我尝试使用mongo语法来做一个LIKE时,它会抛出:

  HTTP / 1.1 400错误请求

是POST请求:

  curl -i -HContent-Type:application / json-X POST -d'{ parameters:{AddressLine1:{$ regex:Rio}},columns:{SalesOrderID:1,TotalDue:1}} https://demo.com/api/v1。 0 / tbles / 56a79d15243c488d217f322c 

  curl -i -HContent-Type:application / json-X POST -d'{parameters:{AddressLine1:{$ regex:/ Rio /}}列:{SalesOrderID:1,TotalDue:1}}'https://demo.com/api/v1.0/tbles/56a79d15243c488d217f322c 


这是应用程序代码:

$ p $ @ app.route( /api/v1.0/tables/<idDatasource>,methods = ['POST'])
def get_table(idDatasource):
return 'Hello'


解决方案

您没有发布有效的JSON。所有字符串值(包括对象中的键)都需要引用。您没有引用 $ regex

$ p $ {parameters:{ AddressLine1:{$ regex:Rio}},columns:{SalesOrderID:1,TotalDue:1}}

如果Flask无法解码JSON数据,则会因为请求无效而产生400错误。


I have a post that works getting a json with the parameters and the columns to get from MongoDB, but when I'm trying to do a LIKE using mongo syntax, it throws:

HTTP/1.1 400 Bad Request

This is the POST request:

curl -i -H "Content-Type: application/json" -X POST -d '{"parameters":{ "AddressLine1":{$regex:"Rio"}},"columns":{"SalesOrderID":1,"TotalDue":1}}' https://demo.com/api/v1.0/tbles/56a79d15243c488d217f322c

or

curl -i -H "Content-Type: application/json" -X POST -d '{"parameters":{ "AddressLine1":{$regex:/Rio/}},"columns":{"SalesOrderID":1,"TotalDue":1}}' https://demo.com/api/v1.0/tbles/56a79d15243c488d217f322c

This is the application code:

@app.route("/api/v1.0/tables/<idDatasource>", methods=['POST'])
def get_table(idDatasource):
    return 'Hello'

解决方案

You didn't post valid JSON. All string values, including keys in objects, need to be quoted. You did not quote $regex.

{"parameters":{"AddressLine1":{"$regex":"Rio"}},"columns":{"SalesOrderID":1,"TotalDue":1}}

If Flask can't decode the JSON data, it raises a 400 error because the request is invalid.

这篇关于将JSON发布到Flask将导致400错误的请求错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆