fastapi
fastapi copied to clipboard
Another request after a response in one endpoint
First Check
- [X] I added a very descriptive title to this issue.
- [X] I used the GitHub search to find a similar issue and didn't find it.
- [X] I searched the FastAPI documentation, with the integrated search.
- [X] I already searched in Google "How to X in FastAPI" and didn't find any information.
- [X] I already read and followed all the tutorial in the docs and didn't find an answer.
- [X] I already checked if it is not related to FastAPI but to Pydantic.
- [X] I already checked if it is not related to FastAPI but to Swagger UI.
- [X] I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- [X] I commit to help with one of those options 👆
Example Code
app.route(‘/api’)
async def api(email: str):
sendmail(mail)
print(“successfully sended”)
another_request(param2: str)
return “2 requests were done”
Description
Like the title and the example says. Is there a way of sending another request(fill another blank input) after the first response? The example doesn’t work but it kind of explain what I want to achieve.
Operating System
Windows
Operating System Details
Windows 11 x64
FastAPI Version
Last one
Python Version
3.9
Additional Context
No response
I am unsure how this should work or how you see this working. Each request in FastAPI is on its own. One request cannot influence another and a response to request A is completely separate from request B.
Maybe if you explain what you are really trying to solve, we could advise a bit on a different design, because conceptually your app is broken if this is a solution I am afraid..
@JarroVGIT let me give a quick draft.
A normal api in localhost/docs is like this.
- An Input that ask for email.
- Using the email, it returns a response.
Now what I want to do is...
- An input that ask for email.
- Using the email, it returns a response.
- Another input that ask for another str after the #2 response was made 4 using the second input, throw another response.
Maybe that will clear up some things
To be honest, that doesn't make it any clearer. It seems to me that you have some confusion on how HTTP requests and response works. In short: a client (let's say a browser) does a request to a server (Uvicorn). It asks (GET) for the response of endpoint "/process_email?user_id=1", Uvicorn calls upon FastAPI to produce a response, and a response is send. The end. There is no notion of something lingering or waiting for maybe a follow up question, that's just it, interaction over.
If you want a client and a webserver to go back and forth with each other, while maintaining the connection, you could use web sockets, where the client and server can keep sending messages to each other.
But normally, how you would implement something like what you want, is to make two distinct requests and responses and handle the logic on the client side (so, not in FastAPI). For example:
- Browser send request to server for
/process_email?user_id=1
- Server sends response like
{"response_unique_id":"some-unique-id"}
- Browser then sends requests to server for
/follow_up_request?unique_id=some-unique-id
- Server handles requests (whatever logic you need) and returns response.
The logic of 'what needs to be the next request' needs to lie at the client, because the server is done after sending the first response. Hope this helps you a bit.
He might be able to achieve something similar to what he is asking with a websocket.
Thought I agree, it feels like a better understanding of REST arch would likely be a better first step.