webhook with fastapi: I am not getting webhooks with Python

Question:

I am trying to get json data on my localhost. But I am getting a problem with Request.json.

main.py

from fastapi import FastAPI, Request
app = FastAPI()
@app.post("/webhook")
async def get_body(request: Request):
    print(request.json)
    return await request.json()

server.py


import requests
import json

webhook_url = "http://127.0.0.1:4000/webhook"

data = { 'name': 'test data', 
         'Channel URL': 'test url' } # myutube channels


r = requests.post(webhook_url, data=json.dumps(data), headers={'Content-Type': 'application/json'})


output

<function Request.json at 0x7ff2d0a878b0>

expected output


{
  "name": "test data",
  "Channel URL": "test url"
}

Asked By: dtc348

||

Answers:

You get back a response object from the requests library. You can try to get the json content using r.json() method.

See docs:
https://docs.python-requests.org/en/master/user/quickstart/#json-response-content

Answered By: Georges Lorré
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.