Reading request
data using orjson
When calling await request.json()
, FastAPI (actually Starlette) first reads the body (using the .body()
method of the Request
object), and then calls json.loads()
(using the standard json
library of Python) to return a dict
/list
object to you inside the endpoint (see the implementation here)βit doesn't use .dumps()
, as you mentioned in the comments section, as that method is used to serialise a Python object into JSON.
Hence, to read/convert the request body using orjson
instead, you can use the below (if you would like to retrieve the raw body within a def
instead of async def
endpoint, please have a look at this answer):
from fastapi import FastAPI, Request
import orjson
app = FastAPI()
@app.post('/')
async def submit(request: Request):
body = await request.body()
data = orjson.loads(body)
return 'success'
Returning response
data using orjson
When returning data such as dict
, list
, etc, FastAPI will automatically convert that return value into JSON, using the Python standard json.dumps()
, after inspecting every item inside and making sure it is serializable with JSON, using the JSON Compatible Encoder (see this answer for more details). Hence, if you would like to use the orjson
library instead, you would need to send a custom Response
directly, as described in this answer. Example:
from fastapi import FastAPI, Request
import orjson
app = FastAPI()
@app.post('/')
async def submit(request: Request):
body = await request.body()
data = orjson.loads(body)
return Response(orjson.dumps(data), media_type='application/json')
Alternatively, you can use the ORJSONResponse
provided by FastAPI (still make sure you have the orjson
libray installed, as well as the content that you are returning is serializable with JSON). Have a look at futher documentation here and here on how to customise and/or set ORJSONResponse
as the default response class (the implementation of ORJSONResponse
can be found here). Example:
from fastapi import FastAPI, Request
from fastapi.responses import ORJSONResponse
import orjson
app = FastAPI()
@app.post('/', response_class=ORJSONResponse)
async def submit(request: Request):
body = await request.body()
data = orjson.loads(body)
return ORJSONResponse(data)
Please make sure to have a look here, here, as well as here and here to learn about the various approaches of sending JSON data to a FastAPI backend, and how to define an endpoint to expect and validate JSON data, instead of relying on using await request.json()
(which is useful when the app requires passing arbitrary JSON data, but does not perform any validation on the data).