Get response body in Middleware
See original GitHub issueHow do I get the response body from the StreamingResponse
object in middleware?
Simple example:
class AccessMiddleware(base.BaseHTTPMiddleware):
async def dispatch(
self,
request: Request,
handler: base.RequestResponseEndpoint,
) -> Response:
response: StreamingResponse = await handler(request)
binary = b''
async for data in response.body_iterator:
binary += data
body = binary.decode()
print(body)
return response # Here body is empty
Is it possible to copy the response.body_iterator
generator?
What is the best way to solve the problem of extracting final data (response body
) and further post-processing?
Thanks.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:7
Top Results From Across the Web
How to read ASP.NET Core Response.Body? - Stack Overflow
In the examples below, I'm trying to get the Response.Body value in a custom middleware class. Response.Body is a set only property in...
Read more >returning-response-body-in-middleware - Next.js
Note: In Next.js v13.0.0 you can now respond to Middleware directly by returning a NextResponse . For more information, see Producing a Response....
Read more >Request and Response operations in ASP.NET Core
This article explains how to read from the request body and write to the response body. Code for these operations might be required...
Read more >Getting response headers and body in middleware? : r/golang
I have been trying to find the best way to receive response headers and body when request are made(also request body).
Read more >Read Request and Response body in ASP.NET Core
Getting started; Create Middleware for Reading the request payload; Enable Middleware in API pipeline; Using ResultFilter. There were a few ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Just because such solution not stated yet, but it’s worked for me:
iterate_in_threadpool
actually making from iterator object async IteratorIf you look on implementation of
starlette.responses.StreamingResponse
you’ll see, that this function used exactly for thisresponse_body[0].decode() Gives error if we return some fields with value of true or false and says “name true is not defined”. How to get rid of this issue ?