Page 1 of 1

Web hook Responses Timing out

PostPosted: Mon Mar 07, 2022 4:42 pm
by SBarnes
We have a customer who is getting the following response from a web hook post out from the api, I am assuming the other side is being found otherwise I would expect to see 404's and we are getting a status of 0 in the response table is there a way to increase the time we give the other side to respond?


Code: Select all
“A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Re: Web hook Responses Timing out

PostPosted: Tue Mar 08, 2022 2:10 pm
by Mike.Sheen
The only way is to modify the REST API code which sends the webhook - this is in the Webhook method of the BusinessLogicPlugin part of the REST API plugin.

in this block of code:

Code: Select all
static async public System.Threading.Tasks.Task Webhook(JiwaFinancials.Jiwa.JiwaApplication.Manager Manager, string body, string eventName, string PluginName)
{      
   string url = Manager.Database.ReadSysData(PluginName, "WebhooksHostURL", "").ToString();
   string clientKey = Manager.Database.ReadSysData(PluginName, "WebhooksClientKey", "").ToString();
         
   if (url.Length > 0)
   {
      try
      {            
         ServiceStack.JsonServiceClient client = new ServiceStack.JsonServiceClient(url);                        
         client.Headers.Add("ClientKey", clientKey);            
         await client.PostAsync<IReturnVoid>(new JiwaFinancials.Jiwa.JiwaServiceModel.WebhooksEventsPOSTRequest() { EventName = eventName, Body = body });            
      }
...


Add a line to set the timeout property of the client before the call to PostAsync:

Code: Select all
         client.Timeout = new TimeSpan(0, 0, 1, 30);

Re: Web hook Responses Timing out

PostPosted: Tue Mar 08, 2022 2:15 pm
by SBarnes
Hi Mike,

Thanks for the reply but where I am trying to increase the timeout is in CallWebhook, namely on


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)
                                                     {
                                                         webRequest.Headers[subscriptionRequestHeader.Name] = subscriptionRequestHeader.Value;
                                                     }
                                                 });
                messageResponse.HTTPCode = 200;
                newStatus = 1;
            }
            catch (System.Exception ex)
            {
                while (ex.InnerException != null) ex = ex.InnerException;

                newStatus = 2;
                messageResponse.Message = ex.Message;
                int statusCode = 0;

                if (ex.GetStatus() != null)
                    statusCode = (int)ex.GetStatus();

                messageResponse.HTTPCode = statusCode;

                if ((statusCode >= 200 && statusCode < 300) || ex.IsAny300() || statusCode == 410)
                    newStatus = 3;  // Permanent fail - we do not attempt to retry these                     
            }

Re: Web hook Responses Timing out  Topic is solved

PostPosted: Tue Mar 08, 2022 2:18 pm
by Mike.Sheen
SBarnes wrote:Hi Mike,

Thanks for the reply but where I am trying to increase the timeout is in CallWebhook


Oh - right you are.

In that case, set the Timeout property of the webRequest to be a value in ms - eg:

Code: Select all
try
{
   // This is the actual POST to the webhook
   subscription.URL.PostStringToUrl(message.Body,
                            requestFilter: webRequest =>
                            {
                               webRequest.Timeout = 3000; // 3000 ms
                               foreach (SY_WebhookSubscriptionRequestHeader subscriptionRequestHeader in subscriptionRequestHeaders)
                               {
                                  webRequest.Headers[subscriptionRequestHeader.Name] = subscriptionRequestHeader.Value;
                               }