Flask(Flask-RESTful)不解析JSON负载 [英] Flask (with Flask-RESTful) not parsing JSON payloads

查看:178
本文介绍了Flask(Flask-RESTful)不解析JSON负载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Angular中创建了一个前端,在Flask中创建了一个RESTful扩展的后端。默认情况下,Angular喜欢发送数据作为有效载荷(例如,这是在Chrome开发人员工具中显示的内容:)。我也知道它可以很容易地将其格式化为一个JSON有效载荷,对于其他终端上的一些其他情况,这将是可取的。



使用什么是最好的方法在RESTful中的参数解析器来处理这个?如果我把东西编码成表格数据,他们可以通过reqparse读取,而不仅仅是像这样的一个原始的有效载荷(虽然从阅读他们的文档和来源,似乎应该能够处理更多的这一点)。我知道,通过在reqparse中使用'location'参数,它会看起来像其他地方(默认情况下它看起来在 form json )。然而,任何不通过表单字段发送似乎并没有被解析,尽管我尝试了任何东西(即使明确设置位置包含请求的所有属性,例如json,args )。示例代码如下所示:

  class Login(restful.Resource):

def __init __(self) :
self.parser = reqparse.RequestParser()
self.parser.add_argument('user',type = str,required = True)
self.parser.add_argument('passw', type = str,required = True)

def post(self):
args = self.parser.parse_args()

这里有一些逻辑
返回self.response

有没有我做错了不能读取参数? / p>

解决方案

您会,我认为,需要执行以下操作:

(1)在 add_argument 中显式 location ='json':

  self.parser.add_argument('user',type = str,location ='json',required = True)
self.parser.add_argument('passw ',type = str,location ='json',required = True)


$ b $ (2)确保来自客户端的http请求的头部字段 Content-Type application / json

  {
user:abc,
passw:123456
}

我经常使用flask-restful解析JSON请求,没有任何问题。一个小的区别是,我没有我的请求解析器作为资源的实例字段。不知道这是否会给你带来麻烦,但如果你仍然陷入困境,可能还是值得一试。


I'm creating a frontend in Angular, and the backend in Flask with the RESTful extension. By default, Angular likes to send data back as a payload (eg this is what it appears like in Chrome Developer tools: ). I am also aware it can easily format this in to a JSON payload, which will be preferable for a few other cases on other endpoints.

What is the best way to use the argument parser in RESTful to deal with this? If I encode things as form data they can be read by reqparse, but not just a raw payload like this (although from reading their documentation and source, it seems like it should be able to handle more than this). I am aware that by using the 'location' arg in reqparse it will look elsewhere (by default it looks in form and json). However, anything not sent via form fields does not seem to be parsed, despite anything I try (Even when explicitly setting location to include every attribute of the request, eg json, args). Sample code appears like:

class Login(restful.Resource):

    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('user', type=str, required=True)
        self.parser.add_argument('passw', type=str, required=True)

    def post(self):
        args = self.parser.parse_args()

        # Some logic here
        return self.response        

Is there something I am doing wrong to not be able to read args?

解决方案

You will, I think, need to do the following:

(1) Make location='json' explicit in add_argument:

self.parser.add_argument('user', type=str, location='json', required=True)
self.parser.add_argument('passw', type=str, location='json', required=True)

(2) Ensure the header field Content-Type of the http request from the client is application/json

(3) The request payload should look like this:

{
  "user": "abc",
  "passw": "123456"
}

I routinely parse JSON requests with flask-restful without any trouble. One small difference is that I don't have my request parser as an instance field of the resource. Not sure if that is causing you trouble, but it might be worth a try to move it out if you are still stuck.

这篇关于Flask(Flask-RESTful)不解析JSON负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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