0
votes

When I try to use app.current_request.json_body in chalice I get a decode error:

Traceback (most recent call last): File "/var/task/chalice/app.py", line 659, in _get_view_function_response response = view_function(**function_args) File "/var/task/app.py", line 34, in post_item data = app.current_request.json_body File "/var/task/chalice/app.py", line 303, in json_body self._json_body = json.loads(self.raw_body) File "/var/lang/lib/python3.6/json/init.py", line 354, in loads return _default_decoder.decode(s) File "/var/lang/lib/python3.6/json/decoder.py", line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/var/lang/lib/python3.6/json/decoder.py", line 357, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

It doesn't matter how simple the data is. Example: {"Company":"ABC"} or {}.

As can be seen in the following code in the API Gateway all I try to do is return the data that has been sent so I don't think this is the problem:

@app.route('/test', methods=['POST'], content_types=['application/json'], cors=cors_config)
def post_item(data):
    data = app.current_request.json_body
    return data

Does anyone know what I might have done wrong?

1
Should your return response be return {'response' : data} ?Woodrow
if we send in sample = {"k": "v"} then return data should return {"k": "v"}Christian Andersson

1 Answers

2
votes

You must remove the data from the parameters of the function. That is used for pass url parameters.

@app.route('/test', methods=['POST'], content_types=['application/json'], cors=cors_config)
def post_item():
    data = app.current_request.json_body
    return data