Emma S
Emma S

Reputation: 41

request.raw_post is returning nil when sending in a webhook

The application I'm working on is currently taking in webhooks from GitHub. We're trying to do something like the following:

raw_payload = request.raw_post
original_payload = JSON.parse(raw_payload)

And then original_payload would be the parsed raw data from the webhook. Everything works locally when I use a curl command to send the webhook: the raw data is found, and the JSON is parsed. However, whenever I put it up on my staging environment and it starts getting real webhooks from GitHub, it starts to break with this error:

TypeError (no implicit conversion of nil into String):
json (1.8.3) lib/json/common.rb:155:in `initialize'
json (1.8.3) lib/json/common.rb:155:in `new'
json (1.8.3) lib/json/common.rb:155:in `parse'

on the line that states JSON.parse(raw_payload), basically meaning that raw_payload is nil. What scenario would have request.raw_post return nil on staging, but work correctly in development?

Thanks in advance for any and all help!

Upvotes: 2

Views: 507

Answers (1)

Emma S
Emma S

Reputation: 41

Figured out my own question. The issue was happening with the whole request.raw_post thing. Although the true reason to why raw_post was returning nil unfortunately remains sort of a mystery (we assume it has something to do with the webhook data being deleted and refreshed before we can grab it), we did find a workaround:

raw_parameters = request.request_parameters

And then we can grab values from raw_parameters like so:

new_request[:github_action] = raw_parameters["action"]

Moreover, this way, if there is no raw_parameters["action"], then nothing will change with new_request. This solved all of our issues!

Sorry I couldn't find the exact reason raw_post was returning nil!

Upvotes: 2

Related Questions