如何从Falcon中的POST请求读取原始JSON请求

时间:2019-06-25 10:42:04

标签: python falconframework

如何解析JSON请求之类的东西?

{
  "location_with_names": [
    {
      "location_id": 101,
      "names": [
        "a",
        "b",
        "c"
      ]
    },
    {
      "location_id": 102,
      "names": [
        "a",
        "e"
      ]
    },
    {
      "location_id": 103,
      "names": [
        "f",
        "c"
      ]
    }
  ]
}

示例代码:

def on_post(self, req, resp):
    location_with_names = req.get_param_as_list('location_with_names')
    print(location_with_names)

location_with_names为无

1 个答案:

答案 0 :(得分:1)

您必须先对其进行反序列化,然后才能对其进行查询。您正在使用的功能用于else entirely。使用stream对象boundedunbound上可用的Request选项。

import json

def on_post(self, req, resp):
    raw_data = json.load(req.bounded_stream)
    location_with_names = raw_data.get('location_with_names')
    print(location_with_names)