How can I access the request headers in a cloud function?

Hi, I’m trying to find a way to access the http request headers in a cloud function, specifically I’m looking for the “X-Forwarded-For” in request headers.
After plowing through the code, it seems that the request object passed to a cloud function is constructed through ‘handleCloudFunction’ in FunctionsRouter, where the only possible extension point is the ‘params’ constructed from combining req.body and req.query, so I’ve tried to register an Express middleware to inject the values I need to either req.body or req.query:

  • Injecting data to req.query doesn’t work, because the logic in ClassesRouter will check for all allowed query values, and error on unrecognized ones.
  • Injecting data to req.body turns out to be beyond my Express middleware knowledge. Apparently the request body is parsed through a JSON body parser registered in ParseServer.js, where the body parser is registered without a “verify” callback, so I can’t find a way to inject any custom logic to that.

Can someone point me to a way to access the HTTP request headers in a cloud function? Thanks!

P.S.: It would nice if we can add a parameter to the ParseServer creation configuration for the “request verifier,” which will be an optional function that get passed along to the JSON body parser’s verify callback, so customizing the requests passed to cloud functions will be a lot easier.

The headers are already available in params. Something like this should work:

Parse.Cloud.define('myFunction', async ({ headers }) => {
  const forwardedFor = headers['X-Forwarded-For']; // Here you have your header
});
1 Like