I was able to get the response of one API from another but unable to store it somewhere(in a file or something before returning the response)
response=RedirectResponse(url="/apiname/")
(I want to access a post request with header and body)
I want to store this response content without returning it.
Yes, if I return the function I will get the results but when I print it I don't find results. Also, if I give post request then I get error Entity not found.
I read the starlette and fastapi docs but couldn't get the workaround. The callbacks also didn't help.
Use the Request object directly For that you need to access the request directly. By declaring a path operation function parameter with the type being the Request FastAPI will know to pass the Request in that parameter.
I didn't exactly get the way to store response without returning using fastapi/starlette directly. But I found a workaround for completing this task.
import requests
def test_function(request: Request, path_parameter: path_param):
request_example = {"test" : "in"}
host = request.client.host
data_source_id = path_parameter.id
get_test_url= f"http://{host}/test/{id}/"
get_inp_url = f"http://{host}/test/{id}/inp"
test_get_response = requests.get(get_test_url)
inp_post_response = requests.post(get_inp_url , json=request_example)
if inp_post_response .status_code == 200:
print(json.loads(test_get_response.content.decode('utf-8')))
Please let me know if there are better approaches.
I have the same problem & I needed to call the third-party API with async way So I tried many ways & I came solution with requests-async library and it works for me.
import http3
client = http3.AsyncClient()
async def call_api(url: str):
r = await client.get(url)
return r.text
@app.get("/")
async def root():
...
result_1 = await call_api('url_1')
result_2 = await call_api('url_2')
...
httpx also you can use this video he is using httpx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With