Friday, 27 August 2021

Getting Error 404 instead of json when trying to use daphne server and pagekite

so I wrote a backend for my website with Python and FastAPI and now want to run it on my computer or phone. To run the python server I decided to use daphne, to make it reachable from a domain name, I've decided to use pagekite.

However, when I go to https://something.pagekite.me:8080/coordinates/?direction_x=1&direction_y=2&gas=2 (not the actual domain name), I get 404 Not found File or directory not found. Sorry! When I go to http://0.0.0.0:8000/coordinates/?direction_x=1&direction_y=2&gas=2 I get an actual json response I expect. Here are the commands I used to run main.py as the backend.

daphne -b 0.0.0.0 -p 8000 main:app

python3 pagekite.py --frontend=something.pagekite.me --service_on=http://0.0.0.0:8000

main.py:

from fastapi import FastAPI
from datetime import datetime
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()


origins = ["*"]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get("/")
def root():
    return {"message": "Hello World"}

directionX = 0
directionY = 0
Gas = 0
@app.get("/coordinates/") #post is used to get data, but I can send it using get and query parameters response_model=Item)
def getcoordinates(direction_x,direction_y,gas): #http://127.0.0.1:8000/coordinates/?direction_x=0&direction_y=10&gas=50
    global directionX,directionY, Gas #changed async def to def 
    directionX = direction_x
    directionY = direction_y
    Gas = gas
    return {"data":(direction_x,direction_y,gas)}

    

I use Linux Mint. Thanks for your help!



from Getting Error 404 instead of json when trying to use daphne server and pagekite

No comments:

Post a Comment