Falcon parsing json error

advertisements

I'm trying out Falcon for a small api project. Unfortunate i'm stuck on the json parsing stuff and code from the documentation examples does not work.

I have tried so many things i've found on Stack and Google but no changes. I've tried the following codes that results in the errors below

import json
import falcon

class JSON_Middleware(object):
    def process_request(self, req, resp):
       raw_json = json.loads(req.stream.read().decode('UTF-8'))
       """Exception: AttributeError: 'str' object has no attribute 'read'"""

       raw_json = json.loads(req.stream.read(), 'UTF-8')
       """Exception: TypeError: the JSON object must be str, not 'bytes'"""

       raw_json = json.loads(req.stream, 'UTF-8')
       """TypeError: the JSON object must be str, not 'Body'"""

I'm on the way of giving up, but if somebody can tell me why this is happening and how to parse JSON in Falcon i would be extremely thankful.

Thanks

Environment: OSX Sierra Python 3.5.2 Falcon and other is the latest version from Pip


your code should work if other pieces of code are in place . a quick test(filename app.py):

import falcon
import json

class JSON_Middleware(object):
    def process_request(self, req, resp):
       raw_json = json.loads(req.stream.read())
       print raw_json

class Test:
    def on_post(self,req,resp):
        pass

app = application = falcon.API(middleware=JSON_Middleware())
t = Test()
app.add_route('/test',t)

run with: gunicorn app
$ curl -XPOST 'localhost:8000' -d '{"Hello":"wold"}'