Minimal example:
#!/usr/bin/env python3
import json
from dataclasses import dataclass
from flask import Flask, request, make_response
from flask_restful import Resource, Api
@dataclass
class Foo:
bar: int
class WebController(Resource):
def __init__(self, factor) -> None:
self._factor = factor
def post(self):
foo = Foo(**json.loads(request.data))
return make_response(str(self._factor * foo.bar), 200)
def main():
app = Flask(__name__)
api = Api(app)
api.add_resource(WebController, "/json_post_endpoint", resource_class_kwargs={"factor": 2})
app.run(port=8080)
if __name__ == "__main__":
main()
Running it, then sending a curl
without the header:
curl -X POST --url http://localhost:8080/json_post_endpoint --data '{ "bar": 42 }'
{"message": "Internal Server Error"}
Logs:
[2023-01-06 10:34:23,616] ERROR in app: Exception on /json_post_endpoint [POST]
Traceback (most recent call last):
File "/home/tobias/.local/lib/python3.10/site-packages/flask/app.py", line 1820, in full_dispatch_request
rv = self.dispatch_request()
File "/home/tobias/.local/lib/python3.10/site-packages/flask/app.py", line 1796, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "/home/tobias/.local/lib/python3.10/site-packages/flask_restful/__init__.py", line 467, in wrapper
resp = resource(*args, **kwargs)
File "/home/tobias/.local/lib/python3.10/site-packages/flask/views.py", line 107, in view
return current_app.ensure_sync(self.dispatch_request)(**kwargs)
File "/home/tobias/.local/lib/python3.10/site-packages/flask_restful/__init__.py", line 582, in dispatch_request
resp = meth(*args, **kwargs)
File "/home/tobias/Documents/main.py", line 20, in post
foo = Foo(**json.loads(request.data))
File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.10/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
But with the header, the curl
is fine:
curl -X POST --url http://localhost:8080/json_post_endpoint --header 'Content-Type: application/json' --data '{ "bar": 42 }'
84
What does adding the header to the request change for the server to behave differently?