Score:0

How to Manage Webhook Notifications?

pe flag

I had developed a controller to retrieve data from an API via a webhook. This is the type of data I received with https://webhook.site for testing :

{
  "owner": {
    "id": "100000",
    "userName": "lorem"
  },
  "notes": "",
  "created": {
    "dateTime": "2021-12-14T18:54:36",
    "timeZone": "Europe/Paris"
  },

Here is a preview of the controller :

    class WebhookLoremController extends ControllerBase {

        public function LoremListener(Request $request) {

            $data_webhook = $request->getContent();
     
            // data is invalid       
            dump($request);
            
            // data is invalid    
            dump($data_webhook);

            // obviously doesn't work as $data_webhook is invalid
            $decode = Json::decode($data_webhook);

         }

    }

Everything was working fine but now the data received is invalid. We can see the @ character for example, or problems with comma, colon, or } :

{
   "owner":"@""id":"100000",
   "userName":"lorem",
   "notes":"",
   "created":"@""dateTime":"2021-12-14T18:54:36",
   "timeZone":"Europe/Paris",

I immediately thought of a problem with the API but I have no problem if I test with https://webhook.site. I take it that the problem is on my side. If I dump $request the problem is already there.

It worked before and I did not change the code. The only thing I'm thinking about is that I updated to Drupal 9 but I'm not sure if that has anything to do with it.

Do I have to do something with drupal to retrieve this kind of data?

EDIT

Here is the dump() of $request :

"POST /webhook/listener-api-lorem-10000 HTTP/1.1 Authorization":"Content-Length":"3607 Content-Type":"application/json Host":"lorem.fr User-Agent":"Jakarta Commons-HttpClient/3.1 X-Agendize-Objectevent":"deleted X-Autonomous-System":"16276 X-Country-Code":"FR X-Forwarded-Port":"443 X-Forwarded-Proto":"https X-Php-Ob-Level":"1 X-Ssl":"yes"{
   "owner":"@""id":"10000",
   "userName":"lorem",
   "notes":"",
   "created":"@""dateTime":"2021-12-14T20:04:36",
   "timeZone":"Europe/Paris"
Jaypan avatar
de flag
The JSON you've shown is invalid. You need to determine if it is coming in invalid, or if something is happening on the Drupal side that is invalidating it. Are you able to view your server request logs to see the raw request data?
pe flag
@Jaypan f I test the webhook with https://webhook.site, the received json is valid. The same webhook received with my controller is not valid. It is exactly the same notification so I was wondering if something special has to be done with Drupal.
Jaypan avatar
de flag
How did you retrieve the invalid JSON you showed in your post? Is that the output of the dump() call?
pe flag
@Jaypan yes, it is the dump from `LoremListener()`, I see the issue in `$request`, so obviously in `$request->getContent()` and of course `Json::decode($data_webhook)` doesn't work It used to work so I don't understand. I ask the API maintainer but they say it is on my side. I'm not ruling out the possibility that the API has been changed.
pe flag
@Jaypan Does the controller seem correct to you?
Jaypan avatar
de flag
What you've got so far looks ok to me. I question the input you are getting, but it's not impossible that Drupal could have transformed it between receiving it, and your controller. But I feel that the value from Request is probably unaltered, which makes me question the incoming data from the API.
pe flag
@Jaypan How to check the data received before the controller to be sure that what I am receiving is correct or not. That's what bothers me to debug. I am blind. A dump tells me that it is not correct but the test with webhook.site is correct (exactly the same notification).
Lambic avatar
ph flag
Is the request coming in with the correct content type? If it's not "application/json" then that could cause issues.
Jaypan avatar
de flag
Incoming server request logs is how I would start.
pe flag
@Lambic I've updated my question with the `dump()`of `$request`. I can see `application/json` It seems correct but I see the invalid datas.
Jaypan avatar
de flag
That looks to me to be an error in the JSON they are sending. I would try contacting them again, and sending the dump of the Request as you've shown above.
pe flag
@Jaypan Thank you for watching. I needed another look at this problem. So I'll get back to them again.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.