Hi
I have successfully configured a route /a/b on my tyk gateway and added a JS pre middleware that will send a post request to another service on my application.
the post request that I send includes the all the data that I can get from the original request /a/b
but it does not include the source ip.
so I have checked the logs of tyk , and they include the source ip , so tyk knows it
also the final request that reaches the target url which is a server that responds to /a/b
the server can see X-FORWARDED-FOR header with the correct value.
while the request object in the middleware does not have that header , so it seems tyk adds it later on
I have seen that there is context variables , but I could not access them
can I access them in JS middleware ? HOW ?
So , The main question is how do I access the origin ip in a JS middleware ?
https://tyk.io/docs/context-variables/
included the middleware js code
var middleware = new TykJS.TykMiddleware.NewMiddleware({});
middleware.NewProcessRequest(function(request, session, spec) {
console.log("Middleware running");
console.log(JSON.stringify(request))
console.log("Middleware running - request log end");
console.log(JSON.stringify(session))
console.log("Middleware running - session log end");
console.log(JSON.stringify(spec))
console.log("Middleware running - spec log end");
console.log('request.Headers["X-Forwarded-For"]')
console.log(request.Headers["X-Forwarded-For"])
console.log('request.Headers["x-forwarded-for"]')
console.log(request.Headers["x-forwarded-for"])
var post_data = {
"method": request.Method,
"headers": request.Headers,
"body": request.Body,
"url": request.URL
};
var newRequest = {
"Method": "POST",
"Body": JSON.stringify(post_data) ,
"Headers": {"Content-Type": "application/json",
"x-remote-addr": "$tyk_context.remote_addr"
},
"Domain": "http://10.0.8.3:3000",
"Resource": "/c/d",
"FormData": {}
};
TykMakeHttpRequest(JSON.stringify(newRequest));
return middleware.ReturnData(request,session.meta_data) //TykJsResponse({Body: "", Code: 200}, session.meta_data);
});
DoProcessRequest = middleware.DoProcessRequest;
console.log("hiyou_middleware.js loaded");