How to dynamically send json body in callback request
See original GitHub issueHello,
I am following to fastAPI callbacks here : https://fastapi.tiangolo.com/advanced/openapi-callbacks/ to try to set up slack alerts in one of my projects.
My goal is to sending a slack message whenever the API request failed. My code looks like this:
from typing import Optional
from fastapi import APIRouter, FastAPI
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Invoice(BaseModel):
id: str
title: Optional[str] = None
customer: str
total: float
class InvoiceEvent(BaseModel):
description: str
paid: bool
class InvoiceEventReceived(BaseModel):
ok: bool
invoices_callback_router = APIRouter()
@invoices_callback_router.post("", response_model=InvoiceEventReceived)
def invoice_notification(body: InvoiceEvent):
return {"okay": "yes"}
@app.post("/invoices/", callbacks=invoices_callback_router.routes)
def create_invoice(invoice: Invoice, callback_url: Optional[HttpUrl] = None):
return {"msg": "Invoice received"}
I would input slack webhooks link in callback_url, however, I am not very sure where to put payload for the callback_url, anyone could help ?
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
How to handle Dynamic JSON in Retrofit? - Stack Overflow
Try custom deserialisation using gson-converter as below(updated answer for Retrofit 2.0) · ResponseWrapper · ResponseMessage · ResponseString
Read more >Fetch data dynamically - Dart
Use HttpRequest to dynamically load data. Web apps often use JSON (JavaScript Object Notation) to pass data between clients and servers. Data can...
Read more >How to pass dynamic JSON request with parameter in POST ...
I have hard coded my json request body and passing values of each fields as variable. EX: { "ProjectId": <var1>, "Address": <var2>,
Read more >How to send a JSON object to a server using Javascript?
So we are using JSON.stringify() function to convert data to string and send it via XHR request to the server. Below is the...
Read more >Creating and Modifying Dynamic Entities | Using JSON
You can use the %ToJSON() method to serialize a dynamic entity (convert it to a JSON string) and the %FromJSON() and %FromJSONFile() methods...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
OpenAPI Callbacks is only used to let your users know, how an external callback API should look like. For making the actual callback request, you will have to use something like HTTPX or Requests. A sample code for sending callback:
Now coming to your use case of Slack alerts, I don’t think you event need to use OpenAPI Callbacks. You could just catch any errors using
try/except
, parse the errors, and send the alert using slackclient. For multiple endpoints you could also use decorators to reduce code duplication.Hope this helps! 😃
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.