Skip to content

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

Description

@BrandeeDev

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions