Flask (with Flask-RESTful) not parsing JSON payloads

后端 未结 1 1870
时光取名叫无心
时光取名叫无心 2021-01-13 01:36

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 appear

相关标签:
1条回答
  • 2021-01-13 02:39

    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.

    0 讨论(0)
提交回复
热议问题