1

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.

MrMinty
  • 53
  • 2
  • 7
  • What do you mean by "do the same POST using just FastAPI's functions? FastAPI is not an API client, is an API server - it does not do outgoing requests. Use `requests`, `httpx` or `urllib3` for that. – MatsLindh Mar 24 '22 at 20:58
  • You might find this answer useful https://stackoverflow.com/questions/63872924/how-can-i-send-an-http-request-from-my-fastapi-app-to-another-site-api – G.M Mar 24 '22 at 20:59
  • I just wanted to make sure that there wasn't a way to make outgoing requests --as I was over the FastAPI's callback documentation https://fastapi.tiangolo.com/advanced/openapi-callbacks/ – MrMinty Mar 24 '22 at 21:06

1 Answers1

0

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'
Ziur Olpa
  • 1,839
  • 1
  • 12
  • 27