Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adhoc fix for a issue on Custom Webhook block: only header no body in the POST request #16870

Closed
wants to merge 1 commit into from

Conversation

zelterNN
Copy link

adhoc fix for a issue on Custom Webhook block: only header no body in the POST request

Checklist

  • This pull request references any related issue by including "closes <link to issue>"
    • If no issue exists and your change is not a small fix, please create an issue first.

This issue is related to the discussion on Slack: https://prefect-community.slack.com/archives/CL09KU1K7/p1737742902411919

  • If this pull request adds new functionality, it includes unit tests that cover the changes
  • If this pull request removes docs files, it includes redirect settings in mint.json.
  • If this pull request adds functions or classes, it includes helpful docstrings.

…art in the POST request for the Custom Web Hook
Copy link

codspeed-hq bot commented Jan 28, 2025

CodSpeed Performance Report

Merging #16870 will not alter performance

Comparing zelterNN:main (96c5152) with main (c685760)

Summary

✅ 2 untouched benchmarks

Copy link
Member

@desertaxle desertaxle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening a PR @zelterNN! Unfortunately, this is a breaking change, so we can't accept it as is.

If you want to input the subject, body, and name info in the body of the payload, you can use the json_data field. The json_data field accepts templated values, so you could do something like this:

{"subject": "{{subject}}", "body": "{{body}}", "name": "{{name}}"}

for your json_data field.

Let me know if that works for your use case!

@zelterNN
Copy link
Author

zelterNN commented Jan 28, 2025

Thanks for opening a PR @zelterNN! Unfortunately, this is a breaking change, so we can't accept it as is.

If you want to input the subject, body, and name info in the body of the payload, you can use the json_data field. The json_data field accepts templated values, so you could do something like this:

{"subject": "{{subject}}", "body": "{{body}}", "name": "{{name}}"}

for your json_data field.

Let me know if that works for your use case!

Hi @desertaxle,

My use case is: sending flow status change notification to Microsoft Teams thru the Automation / Notification thru the Custom Webhook. All configured from the UI.

Here's the issue: on Custom Webhook block itself, the JSON_Data (and other components such as Form Data, Header) is not modifiable. On Automation, there's only Subject and Body that can be filled but they are currenltly fail to generate into the httpx request (what's this PR is trying to solve).

I think there're two issues on the current code:

  • Subject and Body need to be wrapped inside the json argument to be fed in httpx request
  • the apply_values() seems not replacing the default json value (None) in the dict template properly

Looking forward Prefect can have a generic fix onto this in the next version.

Best,
Jason

@desertaxle
Copy link
Member

@zelterNN it's true that you can't modify the json_data when setting up the automation. However, if you set json_data to the value that I suggested when creating the block, then the values for body, subject, and name will be interpolated into the body each time the webhook is triggered.

I tested this and was able to get a body in my webhook that looks like this:

{
  "subject": "Prefect deployment 'sample-v1/rerun_debug' has entered status 'DeploymentStatus.NOT_READY'",
  "body": "\nName: sample-v1/rerun_debug\nStatus: DeploymentStatus.NOT_READY\nURL: None",
  "name": "from-python"
}

I had to create the block in Python because there's an issue with the custom webhook in the UI (#16656), but it seems like it's possible to achieve what you're looking for without any code changes.

@zelterNN
Copy link
Author

@zelterNN it's true that you can't modify the json_data when setting up the automation. However, if you set json_data to the value that I suggested when creating the block, then the values for body, subject, and name will be interpolated into the body each time the webhook is triggered.

I tested this and was able to get a body in my webhook that looks like this:

{
  "subject": "Prefect deployment 'sample-v1/rerun_debug' has entered status 'DeploymentStatus.NOT_READY'",
  "body": "\nName: sample-v1/rerun_debug\nStatus: DeploymentStatus.NOT_READY\nURL: None",
  "name": "from-python"
}

I had to create the block in Python because there's an issue with the custom webhook in the UI (#16656), but it seems like it's possible to achieve what you're looking for without any code changes.

It works! Thank you.

I tried different options when creating the Custom Webhook block from Pythoin, it seems no matter I initiate the block with the json or just the default, as long as the block is created (initiated) from the Python instead of GUI, the block will work as expected.

@zelterNN zelterNN closed this Jan 29, 2025
@zelterNN
Copy link
Author

zelterNN commented Jan 29, 2025

@desertaxle
oops i think i was testing under the test environment (with my ad hoc patch).....

when i move on to production without patch, there's still some issues: if i initiate the block from Python with json supplied, it will work, but only for static string as in your example. If there're dynamic components such as {{ flow.name }}, {{ deployment.name }}, it will failed.

Any workaround on this?

@desertaxle
Copy link
Member

You might need to have the dynamic parts in the body defined in the automation because I don't think the same context is available when templating json_data.

@zelterNN
Copy link
Author

@desertaxle
then we go back to the very beginning, the subject and body defined in the automation won't come into the json_data in the POST.

but you hint me on something, i tried
json_data={'subject': '{{subject}}', 'body': '{{body}}'}
when initializing the webhook block, and define the dynamic part within the automation. And now it works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants