Page 1 of 1

How to add a header in a custom webhook?

PostPosted: Wed Dec 21, 2022 5:17 pm
by sameermoin
Hi Everyone,

I created a custom webhook on inventory creation. Now, I want to add a InventoryId into a header. I check the rest API plugin and their
Code: Select all
ServiceStack.JsonServiceClient client = new ServiceStack.JsonServiceClient(url);
is used to add headers but in my case, I am not creating a request manually just invoking the webhook using the Webhook method.

Code: Select all
string body = inventoryItem.DTO_Serialise().ToJson<JiwaFinancials.Jiwa.JiwaServiceModel.Inventory.InventoryItem>();
            
JiwaFinancials.Jiwa.RESTAPIPlugin.BusinessLogicPlugin.Webhook(inventoryItem.Manager, body, (inventoryItem.InsertFlag) ? "inventory.specialcreated" : (inventoryItem.DeleteFlag) ? "inventory.specialdeleted" : "inventory.specialupdated", JiwaFinancials.Jiwa.RESTAPIPlugin.BusinessLogicPlugin.PluginName);


Any way to add a header from the business logic class?

Re: How to add a header in a custom webhook?

PostPosted: Wed Dec 21, 2022 5:20 pm
by Mike.Sheen
When creating a subscription for the webhook, you can specify the headers per subscription.

So, when you POST to /Webhooks/Subscribers/{SubscriberID}/Subscriptions to create the subscription, your body can add headers like so:

Code: Select all

  "URL": "https://webhook.site/31d2a9ba-649d-447b-8bb1-ae50230f771d",
  "EventName": "salesquote.updated",
  "Headers": [
    {
      "Name": "Content-Type",
      "Value": "application/json"
    }
  ]
}

Re: How to add a header in a custom webhook?

PostPosted: Wed Dec 21, 2022 5:24 pm
by sameermoin
Mike.Sheen wrote:When creating a subscription for the webhook, you can specify the headers per subscription.

So, when you POST to /Webhooks/Subscribers/{SubscriberID}/Subscriptions to create the subscription, your body can add headers like so:

Code: Select all

  "URL": "https://webhook.site/31d2a9ba-649d-447b-8bb1-ae50230f771d",
  "EventName": "salesquote.updated",
  "Headers": [
    {
      "Name": "Content-Type",
      "Value": "application/json"
    }
  ]
}


Its a general header but in my case, I want to add a InventoryId in a header and it would be different for all requests. Do I need to add headers in a body parameter of the Webhook method ?

Re: How to add a header in a custom webhook?  Topic is solved

PostPosted: Wed Dec 21, 2022 5:42 pm
by Mike.Sheen
sameermoin wrote:Its a general header but in my case, I want to add a InventoryId in a header and it would be different for all requests. Do I need to add headers in a body parameter of the Webhook method ?


Ok - I missed that part of your original post.

Note that this:

Code: Select all
JiwaFinancials.Jiwa.RESTAPIPlugin.BusinessLogicPlugin.Webhook(inventoryItem.Manager, body, (inventoryItem.InsertFlag) ? "inventory.specialcreated" : (inventoryItem.DeleteFlag) ? "inventory.specialdeleted" : "inventory.specialupdated", JiwaFinancials.Jiwa.RESTAPIPlugin.BusinessLogicPlugin.PluginName);


Isn't actually sending the webhook to the recipient. It's signaling to the REST API service to send webhooks to any subscriptions there may be for the event. You want to add headers to the POST to all the subscriptions, which occurs in the CallWebhook method of the REST API plugin:

Code: Select all
try
{
   // This is the actual POST to the webhook
   subscription.URL.PostStringToUrl(message.Body,
                            requestFilter: webRequest =>
                            {
                               foreach (SY_WebhookSubscriptionRequestHeader subscriptionRequestHeader in subscriptionRequestHeaders)
                               {
                                 if(subscriptionRequestHeader.Name.ToUpper() == "CONTENT-TYPE")
                                 {
                                    webRequest.ContentType = subscriptionRequestHeader.Value;
                                 }
                                 else
                                 {
                                    webRequest.Headers[subscriptionRequestHeader.Name] = subscriptionRequestHeader.Value;
                                 }
                               }
                            });
   messageResponse.HTTPCode = 200;
   newStatus = 1;
}
catch (System.Exception ex)
{


As you can see there is no opportunity for you to add dynamic headers at this point. I've created improvement DEV-9720 to add a way of doing what you want.

If you can, things will be cleaner and simpler if you can have your webhook receiver accept this information in the body instead of the header.

If you can't, then you could hack the system by adding a custom route in the API to act as the webhook receiver, and it could add your headers or add an event that a plugin could listen to and add the headers there and THAT would send to the webhook recipients. You'd need to change all your webhook subscriber URL's to be the internal custom webhook receiver route and store in the body the real subscriber URL to route to.
It's possible, but not recommended as things will get messy and confusing.

Re: How to add a header in a custom webhook?

PostPosted: Wed Dec 28, 2022 5:21 am
by sameermoin
I think URL also needs to be dynamic. I have a PUT route like https://www.example.com/inventory/:inventoryId to update the specific item, although I have this InventoryId in the Jiwa, but in the current case it's a static URL and all updates are routing on a very specific route.

I tried myself but didn't find a way to do that. If it is possible? please share an example.

Re: How to add a header in a custom webhook?

PostPosted: Tue Jan 03, 2023 10:57 am
by Mike.Sheen
sameermoin wrote:I think URL also needs to be dynamic. I have a PUT route like https://www.example.com/inventory/:inventoryId to update the specific item, although I have this InventoryId in the Jiwa, but in the current case it's a static URL and all updates are routing on a very specific route.

I tried myself but didn't find a way to do that. If it is possible? please share an example.


Mike.Sheen wrote:As you can see there is no opportunity for you to add dynamic headers at this point. I've created improvement DEV-9720 to add a way of doing what you want.