parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

req.headers is undefined

Open nirco123 opened this issue 10 years ago • 14 comments

Hi All,

Any reason why the req.headers is undefined, we would love to get the request IP and it seems impossible without the headers, I tried search the code and the headers are been overwrite, any specific reason for that?

Thanks.

nirco123 avatar Apr 12 '16 09:04 nirco123

Assuming you mean in Cloud Code - this is the case on Parse.com but it is a bit limiting, e.g. we currently send the user agent information as part of a request as it is unavailable in the header.

The request object is not a node request object but Parse.Cloud.FunctionRequest https://parse.com/docs/js/api/classes/Parse.Cloud.html#methods_define I'd also like to know the reasoning behind this.

steven-supersolid avatar Apr 12 '16 09:04 steven-supersolid

Yes, but since we are not on Parse.com hosting any more im trying to understand if there is any reason not using headers in the request .

nirco123 avatar Apr 13 '16 18:04 nirco123

I've anted to do something similar for ages, but until the parse server came along there wasn't a way to do it. I haven't been able to get the headers in the cloud/main.js file so I've added routes that handle the IP detection and then forward that on to the cloud code using Parse.Cloud.run calling the cloud function.

app.post('/push', function(req, res) {
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress;
var data = {ips:ip};
Parse.Cloud.run('functionname', data, {
        success: function(response) {
          res.json({success: true, result:response});
        },
        error: function(error) {
          res.json({success: false, error:error});
        }
      });
}

andyburgess1978 avatar Apr 14 '16 11:04 andyburgess1978

We didn't do this because we were aiming for compatibility with Parse.com. It seems like there is interest, though, and I don't see any reason not to add it.

drew-gross avatar Apr 14 '16 20:04 drew-gross

Any updates on this? I think it's really vital to allow access to the request's headers from Cloud Code.

tarekskr avatar Jun 30 '16 23:06 tarekskr

+1

ritterb82 avatar Jul 13 '16 15:07 ritterb82

+1

greggmojica avatar Aug 17 '16 01:08 greggmojica

@greggmojica this has already been implemented 👍

tarekskr avatar Aug 17 '16 01:08 tarekskr

@tarekskr I actually just saw that. My bad. Thanks!!

greggmojica avatar Aug 17 '16 01:08 greggmojica

@tarekskr the fix posted for #1461 doesn't appear to be true for Triggers. I see the code that adds this for Functions, but the method that headers would be injected for Triggers (before/afterSave, etc) is much more involved and I've spent several hours digging through code to try to find the place to put this in, so if you see a clear path, please provide guidance or help. thx.

crifkin avatar Apr 06 '17 19:04 crifkin

@crifkin sorry for the late response.

Yes it needs more work to be able to forward the request headers to the hooks.

I've prioritized it and we should be able to do something for that.

flovilmart avatar May 06 '17 21:05 flovilmart

@flovilmart I can take this issue, we have a working solution on our local branch and wouldn't take too long to create a pull request

miguel-s avatar Jul 10 '17 14:07 miguel-s

@miguel-s feel free to do so! I’ll review the Pr!

flovilmart avatar Jul 10 '17 14:07 flovilmart

Closing as PR is merged stay tuned for next release

flovilmart avatar Jul 24 '17 20:07 flovilmart