Dev
Dev

Reputation: 13753

How to read raw json request from POST request in Falcon

How do I parse JSON request something like that?

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

sample code:

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

location_with_names is None

Upvotes: 2

Views: 2237

Answers (1)

gold_cy
gold_cy

Reputation: 14236

You'll have to deserialize it first and then you can query it. That function you are using is for something 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)

Upvotes: 4

Related Questions