-1

I am trying to show weather of a specific city but am unable to execute the program. I am using Fastapi. I also included the BaseModel in the solution but still it is not working. What am I doing wrong?

from fastapi import FastAPI, requests
from pydantic import BaseModel

app = FastAPI()

class Data(BaseModel):
    user: str

@app.get("/task1")
def task1(re1: Data):
    re1= requests.get('http://api.openweathermap.org/data/2.5/weather?q=London,uk&appictualappidd=a')
    z = re1.txt
    return z

Command to initiate the server:

uvicorn task:app --host "0.0.0.0" --port 8000 --reload

Output in Browser:

{"detail":"Not Found"}

I was expecting the returned data from the api on browser's screen but am instead getting an error.

Robert
  • 7,394
  • 40
  • 45
  • 64
  • what url do you type in the browser? – kosciej16 Dec 27 '22 at 15:29
  • url was http://localhost:8000 – Muhammad Osama Shahzad Dec 27 '22 at 15:32
  • Does this answer your question? [What is the proper way to make downstream Https requests inside of Uvicorn/FastAPI?](https://stackoverflow.com/questions/73721736/what-is-the-proper-way-to-make-downstream-https-requests-inside-of-uvicorn-fasta) – Chris Dec 27 '22 at 16:56
  • Please have a look at [this answer](https://stackoverflow.com/a/73761724/17865804), as well as the linked answers included in it, on how to POST JSON data to FastAPI backend using JavaScript in the frontend. – Chris Dec 27 '22 at 17:36
  • @Chris this doesn't solve the problem and gives following error. "INFO: 127.0.0.1:55680 - "GET / HTTP/1.1" 422 Unprocessable Entity" Following is shown in the browser: {"detail":[{"loc":["query","user"],"msg":"field required","type":"value_error.missing"}]} – Muhammad Osama Shahzad Dec 29 '22 at 13:41

1 Answers1

0

The problem is defining

def task1(re1: Data):
    ...

expects you will send request body which is no supported in GET requests. Change it to:

def task1(user: str):
    ...

And type http://127.0.0.1:8000/?user=my_user in browser

kosciej16
  • 6,294
  • 1
  • 18
  • 29