Home - hemanth22/fastapi-webhook-receiver GitHub Wiki

Welcome to the fastapi-webhook-receiver wiki!

Test in UAT, yet to plan for prod

from fastapi import FastAPI, HTTPException
from fastapi import Request
from fastapi.responses import JSONResponse

app = FastAPI()

@app.post("/webhook")
async def webhook(request: Request):
    content_type = request.headers.get("Content-Type")

    if content_type == "application/json":
        # Handle JSON payload
        payload = await request.json()
        print("Webhook received (JSON):", payload)
        source = payload["key1"]
        help2 = payload["key2"]
        global CommandCenterResponse
        CommandCenterResponse = f"A message from Command center where '{source}' and '{help2}' reported by '{source}'"
        print(CommandCenterResponse)
        # Handle the JSON payload as needed
    elif content_type == "application/x-www-form-urlencoded":
        # Handle form data
        form_data = await request.form()
        print("Webhook received (Form data):", form_data)
        # Handle the form data as needed
    else:
        raise HTTPException(status_code=400, detail="Unsupported content type")

    return {"status": "Webhook received successfully"}

@app.get("/latest-notification")
async def get_latest_notification():
    return JSONResponse(CommandCenterResponse)
curl -X POST "http://127.0.0.1:8000/webhook" -H "Content-Type: application/json" -d '{"key1": "value1", "key2": "value2"}'

curl -X 'GET' 'http://127.0.0.1:8000/latest-notification' -H 'accept: application/json'