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.

Can I pass body and multipart file into `RedirectResponse` in `post` method?

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

from fastapi import FastAPI

app = FastAPI()


@app.post("/")
def read_root(image_url: Optional[str] = Query('', alias="image_url"), file: UploadFile = File(Required, alias="file")):
    return RedirectResponse("/another")

@app.post("/another")
def read_another_root(image_url: Optional[str] = Query('', alias="image_url"), file: UploadFile = File(Required, alias="file")):
    return {"Hello": "World"}

Description

When the server is requested at / in post method, I want to redirect the /another in post method with same body and multipart file. How can I pass the original body and multipart file into the redirection endpoint?

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.65.2

Python Version

3.8.5

Additional Context

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ghandiccommented, Oct 3, 2021

What is the usecase? You could just add multiple paths for the same endpoint

from fastapi import FastAPI

app = FastAPI()

@app.post("/")
@app.post("/another")
def read_another_root(image_url: Optional[str] = Query('', alias="image_url"), file: UploadFile = File(Required, alias="file")):
    return {"Hello": "World"}
1reaction
ghandiccommented, Oct 3, 2021

I don’t think it is supported in the same way a GET is as the user would need to confirm the redirection

User (Data) -> Server 1 Server 1 (Redirect to Server 2) -> User User - who the heck is server 2… 😃

Since you are not redirecting to another server, you can just call the function directly

from fastapi import FastAPI

app = FastAPI()


@app.post("/")
def read_another_root(demo: str):
    if demo.startswith("a"):
        return do_a(demo)
    return do_b(demo)


@app.post("/a")
def do_a(demo: str):
    return {"answer": "a"}


@app.post("/b")
def do_b(demo: str):
    return {"answer": "b"}

Read more comments on GitHub >

github_iconTop Results From Across the Web

RequestBody and Multipart on Spring Boot - Perficient Blogs
Step 7: Upload the Multipart file and POJO in Postman. Now, Test the response using Postman by adding the body parameters and values...
Read more >
How to redirect multipart POST request to a second server in ...
I am trying to create separate frontend/backend services. Frontend only handles UI related processing, while backend will actually do some ...
Read more >
Exploring the HTTP request syntax | PhpStorm Documentation
To read the request body from a file, type the < symbol followed by the path to the file. // The request body...
Read more >
Custom Response - HTML, Stream, File, others - FastAPI
But if you are certain that the content that you are returning is serializable with JSON, you can pass it directly to the...
Read more >
Documentation - A lightweight Java and Kotlin web framework
Before-handlers are matched before every request (including static files). ... You can add a GET handler to server data to a client, or...
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