Replies: 7 comments
-
|
Not sure if this is a perfect answer, but one thing I suggest is looking at doing the decryption of the body with a pydantic validator. Look for example at the root validators section: https://pydantic-docs.helpmanual.io/usage/validators/#root-validators. In that example the |
Beta Was this translation helpful? Give feedback.
-
|
Thank you for your reply @jjbankert ! from fastapi import FastAPI, Body, Request
from pydantic import BaseModel, root_validator
app = FastAPI()
class DecryptModel(BaseModel):
"""Model that takes care of body decryption."""
@root_validator(pre=True)
def decrypt_body(cls, values):
#decrypt body here
return values
@app.post(path='/use_pydantic')
def use_body(decrypted_body: DecryptModel = Body(...)):
#do stuff
return 'something'When I try to send bytes, I get the same error as described above: The root_validator is only entered when I send the body as a json (e.g. sending {
"detail": [
{
"loc": [
"body",
0
],
"msg": "Expecting value: line 1 column 1 (char 0)",
"type": "value_error.jsondecode",
"ctx": {
"msg": "Expecting value",
"doc": "test",
"pos": 0,
"lineno": 1,
"colno": 1
}
}
]
}For me it seems that FastAPI at some point uses pydantic Is there any way to receive binary body data without validation of any sort? Especially without attempting to decode it as a json? from fastapi import FastAPI, Body, Request
app = FastAPI()
@app.post(path='/get_body')
def get_body(raw_body:bytes = Body(...)):
#decrypt body content and do stuff
return 'something' |
Beta Was this translation helpful? Give feedback.
-
|
@tiangolo I am still stuck with the problem described above. Essentially my question is: Is there a flag like blow: (example not working) @app.post(path='/get_body')
def get_body(raw_body:bytes = Body(... , validation=False)):
#decrypt body content and do stuff
return 'something' |
Beta Was this translation helpful? Give feedback.
-
|
I also have this problem! Someone found some solution? |
Beta Was this translation helpful? Give feedback.
-
|
Did anybody figure out how to solve this? I have the same problem here... |
Beta Was this translation helpful? Give feedback.
-
|
You can use pydantic root_validator with pre=True flag to decrypt your request After this your validator will return argument to input base model. |
Beta Was this translation helpful? Give feedback.
-
|
The solution by @kalmastenitin above is good if only a few of your request types need to be decrypted. But if you need a more general solution to encrypt/decrypt all or most incoming/outgoing messages, you will want to create FastAPI middleware or maybe a dependency. Middleware is quite powerful. It applies to the whole API and allows manipulating raw incoming and outgoing messages without any pydantic validation. Dependencies are simpler and more limited, but can be applied to single routes or path functions. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Description
We are using FastAPI to create an endpoint that receives rsa encrypted data in the request body.
The body consists of binary data (not a json).
We are able to implement the functionality we want, but are struggeling with the documentation and testing in swagger-ui.
What we tried so far
We implemented it along the FastAPI documentation using the starlette
Requestto access the body:The endpoint works (e.g. when using it with Postman), but the usage of the body is not documented in Swagger, and we cannot use Swagger to "try it out" as the body binary file cannot be uploaded anywhere:

According to swagger-ui, we can set the request body content as
application/octet-streamand can then upload a binary file in swagger-ui.We tried to define the Body as a function parameter with
media_type="application/octet-stream":This results in swagger-ui with the desire documentation and ability to upload a binary file for testing:

When using this endpoint structure (with Postman or via swagger-ui), we run into the following error:
{ "detail": "There was an error parsing the body" }Question
How can we both use the testing and documentation functionality of swagger-ui/openapi with our encrypted data?
Is there a way to get the unprocessed body content using FastAPIs
Body()?Environment
Beta Was this translation helpful? Give feedback.
All reactions