Last active
September 13, 2024 03:12
-
-
Save katowulf/6fffffb45ee5cbfbca6c3511e5d19528 to your computer and use it in GitHub Desktop.
Print IP address and headers in Cloud Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require('firebase-functions'); | |
const util = require('util'); | |
exports.helloWorld = functions.https.onRequest((req, res) => { | |
// For Firebase Hosting URIs, use req.headers['fastly-client-ip'] | |
// For callable functions, use rawRequest | |
// Some users have better success with req.headers['x-appengine-user-ip'] | |
const ipAddress = req.headers['x-forwarded-for'] || req.connection.remoteAddress; | |
const headers = JSON.stringify(req.headers, null, 2); | |
const message = util.format("<pre>Hello world!\n\nYour IP address: %s\n\nRequest headers: %s</pre>", ipAddress, headers); | |
res.send(message); | |
}); |
This is still working for me using:
req.headers['x-appengine-user-ip'] || req.header['x-forwarded-for'] || req.connection.remoteAddress
if callable functions, the request headers will be in context.rawRequest.
See documenation: https://firebase.google.com/docs/reference/functions/functions.https.html?authuser=0#.CallableContext
@rolandszpond, thanks for the tip
My req.header
and req.headers
both doesn't have anything. Can u check this please https://stackoverflow.com/questions/57104165/not-all-headers-are-shown-with-firebase-onrequest
I don't know why I have to use res.rawRequest.headers['x-forwarded-for']
2022 and Google still not published any documentation about how to get the client IP.
Increible.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems this has changed recently without any announcements :X