400 Bad Request Subscribing For WebHooks

  • 1
  • Problem
  • Updated 1 month ago
Hi Guys,

I'm using the RestClient to set up a subscription for a WebHook, but I'm just getting a 400 Bad Request exception when call the method to create the subscription. The url I'm using is valid (I've replaced it with a placeholder in the following code for obvious reasons)

Working with a dev sandbox account. Can successfully get an access token and extension list
too so I know that part's working fine.

The iis logs of the WebHook location show it's never hit as part of this process either.

If anyone has any suggestions I'd love to hear them - Thanks ... here's the code ...



GetExtensionListResponse extensionList = await rc.Restapi().Account().Extension().List();

List<string> eventFilters = new List<string>();

foreach (GetExtensionInfoResponse infoResponse in extensionList.records)
{
    eventFilters.Add($"/restapi/v1.0/account/~/extension/{infoResponse.id}/incoming-call-pickup");
    Console.WriteLine($"Extension number / ID : {infoResponse.extensionNumber} / {infoResponse.id}");
}

try
{
    SubscriptionInfo subscriptionInfo = await rc.Restapi().Subscription().Post(new CreateSubscriptionRequest()
    {
        eventFilters = eventFilters.ToArray(),
        deliveryMode = new NotificationDeliveryModeRequest()
        {
            transportType = "WebHook",
            address = "http://my.url.com/MyWebHook";
        }
    });
}
catch (Exception ex)
{
    Console.WriteLine(ex); // *** Catching 400 Bad Request exception here ***
}
Photo of Matt Resmer

Matt Resmer

  • 300 Points 250 badge 2x thumb

Posted 1 month ago

  • 1
Photo of Brandon Hein

Brandon Hein

  • 418 Points 250 badge 2x thumb
I'm curious... is your webhook listener set up to echo back the 'validation-token' header in the initial call response?

I saw this error before and once I coded my webhook listener controller to echo back that header I started seeing success. (Be mindful tho... this header is only used on the initial call to validate your webhook endpoint)

Check this out: https://ringcentral-quickstart.readth...

Ring Central does this so they know your webhook is who you tell them it is... and not some random black hole they send data to.
Photo of Matt Resmer

Matt Resmer

  • 300 Points 250 badge 2x thumb
Hey, thanks Brandon.

No, it's not set up to do that yet. But shouldn't I still see a hit to that endpoint in the iis log?

Also, that 'validation-token' ... is that the VerificationToken that gets passed in as part of the subscription request or is it something else?
Photo of Tyler Long

Tyler Long, Official Rep

  • 9,336 Points 5k badge 2x thumb
After you catch the exception, try to get more info out of it.

catch (FlurlHttpException fhe)
{
    string errorMessage = fhe.GetResponseString();
    Console.WriteLine(errorMessage);
}


If the exception you caught is not  FlurlHttpException, try to inspect its inner exception.
Photo of Brandon Hein

Brandon Hein

  • 418 Points 250 badge 2x thumb
It's something else. And I'm not quite sure. I'm sure a RC expert can explain that a bit more. And maybe why the iis logs dont show anything. They're sending a specific RC user agent as well, not sure if that helps.

For others that stumble across this thread... think of this flow webhook subscription flow like this:
1. You send a request to subscribe via webhook.
2. As soon as RC gets that request they send a POST api call with an empty api body and a 'validation-token' to the url you requested
3. If your webhook url is set up to respond with a 200 OK and the same 'valdiation-token' header in the response as was the request... you'll get a 200 OK from the call in step 1.
4. If you have a bad url... it times out... and/or doesn't echo that header back... you'll get a 4xx error
Photo of Matt Resmer

Matt Resmer

  • 300 Points 250 badge 2x thumb
Phew, got it! ... Thanks so much for the help people :)

That last post of yours Brandon gave me the last couple of bits and pieces I needed, thanks