1

I built my first API using FastAPI and after completing development and deployed my application using an Ubuntu server. I have also set-up NGINX & SSL.

I now need to populate my database with information that I already have available, and I figured that the best way to do so in bulk was through a python script (I will have more than 1000 records to post). During production, I had set up my script and it was working perfectly but now I can't get it to work in development. I've tried a hundred different ways but the post request gets redirected to a GET request and the response is a 200 OK message rather than a 201 created message. What's even more perplexing is that POST requests are working when done though Postman, and then when I use Postman to get the code snippet in python it does not work. enter image description here

This my app: https://github.com/andreasmalta1/football_data_api.git This is where the app is hosted: https://thefootballdata.com/api/teams/

This my script to send the POST request:

import requests
import json

login_url = "https://thefootballdata.com/api/login"
post_url = "https://thefootballdata.com/api/teams"

login_response = requests.post(login_url, data=login_payload)
access_token = login_response.json()["access_token"]

payload = json.dumps({
  "full_name": "Andreas Calleja",
  "name": "Andreas"
})

headers = {
  'Authorization': f"Bearer {access_token}",
  'Content-Type': 'application/json'
}

response = requests.request("POST", url=post_url, headers=headers, data=payload)

print(response.text)
ancalleja
  • 57
  • 5
  • 1
    Does this answer your question? [How to post JSON data to FastAPI and retrieve the JSON data inside the endpoint?](https://stackoverflow.com/questions/70975344/how-to-post-json-data-to-fastapi-and-retrieve-the-json-data-inside-the-endpoint) – Chris Jan 04 '23 at 12:39
  • 1
    Please have a look at [this answer](https://stackoverflow.com/a/70636163/17865804) as well. – Chris Jan 04 '23 at 12:40
  • Sincere apologies Chris. I do appreciate the effort. Unfortunately, the provided links did not provide a working solution. It's quite difficult to provide a minimal reproducible example as the app is live and even more so due to security issues. I'll figure out the best way to do this however. – ancalleja Jan 04 '23 at 13:03
  • Oh wow Chris. That seems to have done the trick. Seems quite interesting that there is no need to do this in Postman or even during development but this solves it. Thanks a lot for your help and sorry for the trouble caused. – ancalleja Jan 04 '23 at 13:39
  • 1
    I would also suggest you have a look at [this answer](https://stackoverflow.com/a/71517830/17865804), which provides important information on `async/await`, as well as a Python example using the `httpx` lib to excute multiple asynchronous requets concurrently, in case that would help with testing your app. – Chris Jan 04 '23 at 13:52
  • Done. I had an inkling that either Nginx or SSL were causing the issues but wasn't sure. Time to do some further research. Thanks again Chris. – ancalleja Jan 04 '23 at 13:59

1 Answers1

1

As pointed out by Chris, since my API allows both GET and POST requests at the same endpoint (api/teams), the solution was to put a trailing / at the request URL. Throughout my testing this was not needed in Postman and during development, but only during production scripting.

Chris
  • 18,724
  • 6
  • 46
  • 80
ancalleja
  • 57
  • 5