如何在 Falcon 中从 POST 请求中读取原始 json 请求

How to read raw json request from POST request in Falcon

如何解析 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 是 None

你必须先反序列化它,然后你才能查询它。您正在使用的那个功能是为了 else entirely. Use the stream options available on the Request object, bounded or unbound.

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)