Allow option to disable the automatic body parsing in Gatsby Functions #32862
-
Some APIs require the original |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 11 replies
-
Have we seen any updates on this? I am currently trying to solve this problem and am unable to find any solutions. |
Beta Was this translation helpful? Give feedback.
-
This is caused by the function addRawBody(req, res, next) {
req.setEncoding('utf8');
var data = '';
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function() {
req.rawBody = data;
next();
});
} A more flexible and powerful solution would be reading a function addRawBody(req, res, next) {
// Get function
const { "0": pathFragment } = req.params
const { functions }: { functions: Array<IGatsbyFunction> } =
store.getState()
// Get function object.
let functionObj = functions.find(
({ functionRoute }) => functionRoute === pathFragment
)
// Check for config. If not using rawBody option, just use express middleware.
if(!functionObj.config.rawBody){
express.json(req, res, next)
} else {
// Otherwise set the body as raw
req.setEncoding('utf8');
var data = '';
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function() {
req.body = data;
next();
});
}
} Realistically, adding a config object would involve more planning and documentation, but is a long term better solution. |
Beta Was this translation helpful? Give feedback.
-
Something like:
|
Beta Was this translation helpful? Give feedback.
-
Just popping in to say that I just spent a couple hours trying to figure out why my code wasn't running with the Stripe webhooks API - and found this thread eventually. I wasn't expecting Gatsby to be parsing the incoming data - but also makes sense when I think about it. +1 for creating a config option to disable this. |
Beta Was this translation helpful? Give feedback.
-
I've just run into issues creating webhooks for Discord. Like Stripe, it also requires validating the signature, and needs access to the |
Beta Was this translation helpful? Give feedback.
-
Yeah for shipping this, however I cannot seem to get it to work. I followed the docs and this is my code is my code: queen-raae/gatsby-funcjam-21#4. |
Beta Was this translation helpful? Give feedback.
-
I think @EzraEllette worked on this one, any ideas? @raae First comment says working in develop, but second one implies it doesn't, can you clarify? |
Beta Was this translation helpful? Give feedback.
-
This is released now in gatsby 4.14 |
Beta Was this translation helpful? Give feedback.
This is released now in gatsby 4.14