如何从fastapi中的另一个API调用一个API? [英] How to call an api from another api in fastapi?
问题描述
我能够从另一个API获得一个API的响应,但是无法将其存储在某个地方(返回响应之前存储在文件或其他内容中) response = RedirectResponse(url ="/apiname/")
(我要访问带有标题和正文的帖子请求)
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.
是的,如果我返回该函数,我会得到结果,但是当我打印它时,我找不到结果.另外,如果我发出发布请求,则会收到错误Entity not found.
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.
我阅读了starlette和fastapi文档,但无法解决.回调也无济于事.
I read the starlette and fastapi docs but couldn't get the workaround. The callbacks also didn't help.
推荐答案
在没有直接返回使用 fastapi/starlette 的情况下,我没有完全找到存储响应的方法.但是我找到了完成此任务的解决方法.
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.
这篇关于如何从fastapi中的另一个API调用一个API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!