Handling requests with empty Content-Type

Hi, I want to receive a 3rd party webhook and do something with it. However, the webhook does not have a Content-Type header. The BodyParser middleware seems to rely on a Content-Type header as one can see in its handle() method. The result is that I can’t access the payload via request.all() or request.body() or request.input() in my controller. It is all empty.

As soon as I synthetically set the Content-Type header to application/json it is working.

Can’t the body parser guess the content type if the header is missing? Or at least assume text/plain?

What can I do right now in my situation?

1 Like

Hello @rostockahoi. I guess you already have a solution to your problem. Set the content header to application/json programmatically as you’ve tried already.

Hey @ndianabasi, thanks for your reply. Unfortunately this is not a solution since I captured and replayed the modified request with Postman. And I obviously have no control over the 3rd party API webhook.

What I need is settings content header within AdonisJS. (I think this wonÄt work since the Request class is immutable) Or can I swap out the BodyParser for this particular request?

Can you try to fork the BodyParser middleware and add your forked version to Adonis? Then make some modifications to the source code of the forked BodyParser middleware so that your request can be properly parsed. From there you can create a PR.

Or you could visit the Discord server and ask ask @virk. He’s active there.

I’d create middleware to handle it for me and then attach this middleware to only this route. I’m not sure if you could set header in there and it would start to work magically.
If that fails could parse raw response in middleware and attach it to request for later use in controllers