How to get request body as bytes from Starlette request object in Flask function? #2016
Unanswered
chrisinmtown
asked this question in
Q&A
Replies: 1 comment
-
Ah, sorry I somehow overlooked this but maybe I can help here as I did the same (also using flask). In the end, this was quite simple. I am using a fully synchronous app. OpenAPI Spec: paths:
/users/{user_id}/documents:
post:
summary: store a new document
security:
- ApiKeyAuth: []
operationId: "document_upload"
parameters:
- in: path
name: user_id
required: true
schema:
type: integer
requestBody:
description: PDF document
required: true
content:
multipart/form-data:
schema:
type: object
properties:
pdf:
type: string
format: binary Accessing the body in the document is really, really simple: def document_upload(user_id, pdf):
pdf_bytes = pdf.read() Of course I don't dare to admit how many hours I needed to discover this :-) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Please have patience with a noob sync/async question. I'm migrating a Connexion v2 app to v3. The old app accesses request data using
request.data
. That is no longer possible in Connexion v3, the request object is typestarlette.requests.Request
and has new ways to get the data. The doc at https://www.starlette.io/requests/ says:I am pretty sure I have a plain, synchronous Flask app function, and the
async
definition means I have to call that async functionbody
appropriately. I asked the cloud about calling an async function from a sync function. I found https://stackoverflow.com/questions/51762227/how-to-call-a-async-function-from-a-synchronized-code-python which, if I am interpreting it correctly, means I should do something like this:Just would like to confirm with an async expert, whether that is the right thing to do inside a function invoked by Connexion. I don't want to break the app in some subtle way out of sheer ignorance.
Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions