question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to download though @app.post using StreamingResponse

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

app = FastAPI()

origins = ["*"]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.post("/download_test")
async def admin_download_domain_file(request: Request):
    import xlsxwriter
    from fastapi.responses import StreamingResponse, FileResponse
    from io import BytesIO

    output = BytesIO()
    workbook = xlsxwriter.Workbook(output)
    worksheet = workbook.add_worksheet()
    worksheet.write(0, 0, "test")
    workbook.close()
    headers = {"Content-Disposition": "filename=filename.xlsx"}
    return StreamingResponse(output, headers=headers)

Description

I can’t download file though @app.post, but if i change it to @app.get, it works fine. The reason I need to use @app.post is because I need to send a list to server, maybe hundreds of url in it then return a xlsx file which contains their imformation. I got these –Failed to fetch. –Possible Reasons: –CORS –Network Failure –URL scheme must be “http” or “https” for CORS request. in http://127.0.0.1:8000/docs And everytime I use POST method it will jump to GET method like this: INFO: 127.0.0.1:44548 - “POST /download_test HTTP/1.1” 200 OK INFO: 127.0.0.1:44688 - “GET /download_test HTTP/1.1” 405 Method Not Allowed But I didn’t define GET method. I have set allow_origins=[‘*’], it does’t work.

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.66.1

Python Version

3.9.5

Additional Context

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
skk294commented, Aug 14, 2021

@Dustyposa Thanks for your help. According to your words, I finally realize it’s the XDM Browser Monitor’s problem, it somehow make me jump to GET method. After shutting it down, everything works fine.

0reactions
jramirez92commented, Apr 13, 2022

I’m having the same issue, Is there any way to update the Swagger version in FastApi?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to download a large file using FastAPI? - Stack Overflow
Did you try setting media_type='application/octet-stream' for the StreamingResponse to indicate that it's binary data? Do you have the example code that fails?
Read more >
Implement HTTP Streaming with Node.js and Fetch API
We're going to go through how we can use HTTP streaming in our applications and what to consider when we do so. Introduction....
Read more >
API — Flask Documentation (2.2.x)
When handling a request, the application context is popped after the request context. See do_teardown_request() . This calls all functions decorated with ......
Read more >
How to stream to an HTTP response - Mario Kandut
In this tutorial we are going to use streams to efficiently send a file as an HTTP response and download it. The big...
Read more >
Received & return a file from in-memory buffer using FastAPI
How to receive a file to the in-memory buffer and then return the file ... import StreamingResponse from io import BytesIO app =...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found