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"
}
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
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"
}
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