I've been trying to figure out how to properly do a POST with FastAPI.
I'm currently doing a POST with python's "requests module" and passing some json data as shown below:
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)
I don't understand how to do the same POST using just FastAPI's functions, and reading the response.
CodePudding user response:
The module request is not a FastAPI function, but is everything you need, first you need to have your FastAPI server running, either in your computer or in a external server that you have access.
Then you need to know the IP or domain of your server, and then you make the request:
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
Your FastApI script would look like this, and should be running while you make your request:
from fastapi import FastAPI
app = FastAPI()
@app.post("/send_some_info")
async def test_function(dict: dict[str, str]):
# do something
return 'Success'