question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to dynamically send json body in callback request

See original GitHub issue

Hello,

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:closed
  • Created 2 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
AmeyMore98commented, Apr 18, 2021

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:

callback_url = "https://example.com/api/v1/callback"
callback_payload= {"detail": "Request processed"}
requests.post(callback_url, json=callback_payload)

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! 😃

0reactions
github-actions[bot]commented, Dec 11, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found