fastapi-gateway icon indicating copy to clipboard operation
fastapi-gateway copied to clipboard

route throw error when it deserialize a StreamingResponse/FileResponse (Fastapi)

Open BrandeeDev opened this issue 3 years ago • 4 comments

Hello. I'm having trouble downloading PDF files from the microservice. I suppose that the maqueRequest method only decodes JSON responses. In my example, the gateway throws an exception since the microservice returns a StreamingResponse.

with async_timeout.timeout(delay=timeout):
        async with aiohttp.ClientSession(headers=headers) as session:
            async with session.request(
                    method=method, url=url, params=query, data=data) as response:
                response_json = await response.json()
                decoded_json = decode_json(data=response_json)
                return decoded_json, response.status, response.headers

My method signature

@pmrush_billing_information_router.get("/api/billing/invoices/download/{invoice_number}", response_model=Any,
                                          tags=["billing_information"])
   @pm_logger
   async def download_invoice(self, invoice_number: str, auth_user: UserInfoResponse = Depends(get_current_user)):

      ......

       invoice_pdf = requests.get(download_link, stream=True)
       output = BytesIO(invoice_pdf.content)
       output.seek(0)
       headers = {
           'Content-Disposition': 'attachment; filename="invoice.pdf"'
       }
       return StreamingResponse(output, headers=headers)

Api gateway signature:

@staticmethod
   @route(
       request_method=api_gateway_billing_information_router.get,
       service_url=SERVICE_URL,
       gateway_path='/api/billing/invoices/download/{invoice_number}',
       service_path='/api/billing/invoices/download/{invoice_number}',
       query_params=["invoice_number"],
       body_params=[],
       status_code=status.HTTP_200_OK,
       tags=['billing_information'],
       response_model=Any
   )
   @pm_logger
   async def download_invoice(invoice_number: str, request: Request, response: Response,
                              token: str = Depends(reuseable_oauth)):
       pass

BrandeeDev avatar Nov 01 '22 21:11 BrandeeDev

Hello Any update for this feature ?

BrandeeDev avatar Nov 03 '22 16:11 BrandeeDev

Hello Any update for this feature ?

Hello, I'll take a look the other day, I left the city :(

dotX12 avatar Nov 03 '22 22:11 dotX12

@BrandeeDev Hi, I was somehow able to add support for SteamingResponse. Look in the Pull request #7

xitowzys avatar Nov 28 '22 07:11 xitowzys

Hallo, this feature is CRITICAL in many scenarios... a lot of people need to download - through the API gateway - a binary resource... can you please pay attention to our requests?

ricciarellif avatar Sep 25 '23 06:09 ricciarellif