python-3.xserverless-offlinefastapi

No response from any API routes when using Serverless-offline plugin


from mangum import Mangum
from fastapi import FastAPI


app = FastAPI()


@app.post("/items/")
def create_item(item_id: int):
    return {"id": item_id}


@app.get("/items/")
def list_items():
    items = [{"id": i} for i in range(10)]
    return items


@app.get("/")
def read_root():
    return {"Hello": "World!"}


handler = Mangum(app)

I'm using the above code when specified in main.py and it's configured with a catch-all route in serverless.yml. I want to test it locally using the serverless-offline plugin but when I run the offline plugin using sls offline, I don't get any response in the browser for any routes. It just says, localhost didn’t send any data.

What could I be doing wrong? The offline plugin is listening on port 3000 by default.


Solution

  • I realized that I hadn't exposed port 3000 from my Docker image. Doing that immediately solved my problem.