簡體   English   中英

使用 FastAPI 發布到外部 url

[英]POST to external url with FastAPI

我一直在嘗試弄清楚如何使用 FastAPI 正確執行 POST。

我目前正在使用 python 的“請求模塊”進行 POST 並傳遞一些 json 數據,如下所示:

import requests
from fastapi import FastAPI

json_data = {"user" : MrMinty, "pass" : "password"} #json data
endpoint = "https://www.testsite.com/api/account_name/?access_token=1234567890" #endpoint
print(requests.post(endpoint, json=json_data). content)

我不明白如何僅使用 FastAPI 的函數並閱讀響應來執行相同的 POST。

模塊請求不是 FastAPI function,而是您需要的一切,首先您需要讓 FastAPI 服務器在您的計算機或您有權訪問的外部服務器中運行。

然后你需要知道你的服務器的 IP 或域,然后你發出請求:

import requests
some_info = {'info':'some_info'}
head = 'http://192.168.0.8:8000' #IP and port of your server 
# maybe in your case the ip is the localhost 
requests.post(f'{head}/send_some_info', data=json.dumps(tablea))
# "send_some_info" is the address of your function in fast api

您的 FastApI 腳本將如下所示,並且應該在您發出請求時運行:

from fastapi import FastAPI
app = FastAPI()

@app.post("/send_some_info")
async def test_function(dict: dict[str, str]):
    # do something
    return 'Success'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM