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

Discussions relating to plugin development, and the Jiwa API.

How to add a header in a custom webhook?

Postby sameermoin » Wed Dec 21, 2022 5:17 pm

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?
sameermoin
Regular Contributor
Regular Contributor
 
Posts: 75
Joined: Wed Jun 15, 2022 4:02 am
Topics Solved: 1

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

Postby Mike.Sheen » Wed Dec 21, 2022 5:20 pm

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"
    }
  ]
}
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

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

Postby sameermoin » Wed Dec 21, 2022 5:24 pm

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 ?
sameermoin
Regular Contributor
Regular Contributor
 
Posts: 75
Joined: Wed Jun 15, 2022 4:02 am
Topics Solved: 1

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

Postby Mike.Sheen » Wed Dec 21, 2022 5:42 pm

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.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

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

Postby sameermoin » Wed Dec 28, 2022 5:21 am

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.
sameermoin
Regular Contributor
Regular Contributor
 
Posts: 75
Joined: Wed Jun 15, 2022 4:02 am
Topics Solved: 1

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

Postby Mike.Sheen » Tue Jan 03, 2023 10:57 am

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.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756


Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 46 guests

cron