Skip to content

Webhook with POST and GET methods at the sametime #2738

@pedrojunqueira

Description

@pedrojunqueira

Hi I created a route in Flask app to handle webhook which is accept both POST and GET request and will handle it depending on the request method sent to the route.

here is the "happy path" of a Flask route

@app.route('/webhook', methods=['GET','POST'])
def webhook():
    if request.method == 'POST':
        # request.json #dosomething
        return 'EVENT_RECEIVED' , 200
    else:

        response = dict()

        response["hub.challenge"] = request.args.get('hub.challenge')

        return response, 200

I am trying the same with FAST API using the Request starlette class and the Basemodel I wanted to make both optional and handle separately. Like checking the Request object first if not then get the request body of a post request.

@app.post("/webhook")
@app.get("/webhook")
async def webhook(request: Request, event: Event):

    challenge = request.query_params.get('hub.challenge')

    if challenge:
        return {"hub.challenge": challenge}


    event #dosomething
    return 'EVENT_RECEIVED' , 200

Here is a get request example

import requests

url = "http://127.0.0.1:8000/webhook?hub.verify_token=STRAVA&hub.challenge=15f7d1a91c1f40f8a748fd134752feb3&hub.mode=subscribe"

r = requests.get(url)

Here is a post request example

import requests

event = {
    "aspect_type": "update",
    "event_time": 1516126040,
    "object_id": 1360128428,
    "object_type": "activity",
    "owner_id": 134815,
    "subscription_id": 120475,
    "updates": {
        "title": "Messy"
    }
}

r = requests.post("http://127.0.0.1:8000/webhook", json=event)

The post is working but when I send a get request here is the IO response

{'date': 'Mon, 01 Feb 2021 11:29:28 GMT', 'server': 'uvicorn', 'content-length': '81', 'content-type': 'application/json'}
{"detail":[{"loc":["body"],"msg":"field required","type":"value_error.missing"}]}
422

FastAPI Logs

INFO:     127.0.0.1:56956 - "GET /webhook?hub.verify_token=STRAVA&hub.challenge=15f7d1a91c1f40f8a748fd134752feb3&hub.mode=subscribe HTTP/1.1" 422 Unprocessable Entity

Thanks for assistance on this

:)

First check

  • [ X] I added a very descriptive title to this issue.
  • [ X] I used the GitHub search to find a similar issue and didn't find it.
  • [ X] I searched the FastAPI documentation, with the integrated search.
  • [ X] I already searched in Google "How to X in FastAPI" and didn't find any information.
  • [ X] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [ X] I already checked if it is not related to FastAPI but to Pydantic.
  • [ X] I already checked if it is not related to FastAPI but to Swagger UI.
  • [ X] I already checked if it is not related to FastAPI but to ReDoc.
  • [ X] After submitting this, I commit to one of:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions