Not able to save cookies from proxy response to the session.
const proxyOptions = {
target: targetUrl, // target host
pathRewrite: function (path) {
return path.replace('/api', '')
},
logLevel: 'debug',
onProxyReq: function (proxyReq, req, rsp) {
proxyReq.path = req.path + '?ticket=' + req.session.pt[targetUrl]
if(req.session.cookie.pt_session_id && req.session.cookie.pt_session_id[targetUrl]) {
proxyReq.writeHead(req.session.cookie.pt_session_id[targetUrl])
}
},
onProxyRes: function onProxyRes(proxyRes, req, res) {
console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2))
req.session.cookie.pt_session_id = req.session.cookie.pt_session_id || {}
req.session.cookie.pt_session_id[targetUrl] = proxyRes.headers['set-cookie']
}
}
var apiProxy = proxy('/api', proxyOptions);
var app = express();
app.use(apiProxy);
app.listen(3000);
Hi,
Thanks guys for the awesome middleware which made my life very easy. We need minor information regarding my usecase. We are implementing CAS proxy using then middleware. Our cas server behaviour is it, first time it accept the proxy ticket and provides the _session_id, it expects that _session_id in further request. I am not able to save it to session and use it. Have I missed something. ?
proxy response looks like below:
RAW Response from the target {
"server": "nginx/1.6.2",
"date": "Fri, 13 May 2016 06:38:26 GMT",
"content-type": "application/json;charset=utf-8",
"content-length": "2071",
"connection": "close",
"status": "200 OK",
"set-cookie": [
"_session_id=randombignumber; path=/; HttpOnly"
]
}
You can try to relay all headers back and forth.
Hopefully this will take care of you cookie issue.
const proxyOptions = {
onProxyReq: relayRequestHeaders,
onProxyRes: relayResponseHeaders
}
function relayRequestHeaders(proxyReq, req) {
Object.keys(req.headers).forEach(function (key) {
proxyReq.setHeader(key, req.headers[key]);
});
}
function relayResponseHeaders(proxyRes, req, res) {
Object.keys(proxyRes.headers).forEach(function (key) {
res.append(key, proxyRes.headers[key]);
});
}
Please use StackOverflow to troubleshoot your setup if you need further assistance.
Thanks.
@chimurai : thanks for the response, it works. will use stack overflow for further information.
Good to hear it works.
Credits should go to https://github.com/Yomguithereal/kotatsu/pull/106 for providing this solution.
Can you link the StackOverflow thread to this one (and vice versa); In case other people run into the same issue?
Thanks.
@chimurai I'm having a problems with "res.append", I got error "has no method ".
My project use _browser-sync_ and I check that this dependency has one dependency that use an old version of _express_ 2.5.11 and the method "append" gives the error above.
How can I fix it?
Error description:
res.append(key, proxyRes.headers[key]);
^
TypeError: Object [object Object] has no method 'append'
@dcbasso res.append is introduced in Express v4.11
More info:
https://github.com/expressjs/expressjs.com/issues/312
@chimurai I know, I'm using this version, but the browsersync have some dependencys that use a old version of the express! Appears to have a conflict!
If you believe it is an issue related to Browser-Sync, please open a ticket at: https://github.com/browsersync/browser-sync/issues
I not really sure... I just can麓t understand why this is happens... why my object don't have the information that I need, if i for to use the correct vesion in "package.json".
Could be that I said before? Can be another thing?
It doesn't sound like a http-proxy-middleware bug.
Think StackOverflow would a better place to troubleshoot your personal setup.
Hi, I am facing the same issue but I don't know how to convert this solution to my bs-config.js (lite server)
http://stackoverflow.com/questions/38950292/http-proxy-middleware-how-to-copy-all-cookie-headers
@MadUser. What is the issue you're facing exactly?
Like the original post. I need to pass the session cookie back and forth but my config file looks nothing like the original post. I added link to stack overflow.
I had a similar problem with front end rewriting /api path to /. e.g. http://www.website.com/api/whoami would be forwarded to http://api.website.com/whoami, but api.website.com would set cookies with the path param not prefixed with /api.
Solved with the following code (reads set-cookie from incoming target response and prefixes cookie path params with /api:
import cookiejar from 'cookiejar'
app.use('/api', proxy({
target: 'http://api.domain.com',
pathRewrite: { '^/api': '' },
onProxyRes: (proxyRes) => {
// prepend /api to cookie paths
const setCookieHeaders = proxyRes.headers['set-cookie'] || []
const modifiedSetCookieHeaders = setCookieHeaders
.map(str => new cookiejar.Cookie(str))
.map(cookie => {
if (cookie.path && cookie.path[0] === '/') {
cookie.path = `/api${cookie.path}`
}
return cookie
})
.map(cookie => cookie.toString())
proxyRes.headers['set-cookie'] = modifiedSetCookieHeaders
},
changeOrigin: true,
}))
Would be nice if this was a configurable option for http-proxy-middleware since it is probably a fairly common scenario.
Most helpful comment
I had a similar problem with front end rewriting
/apipath to/. e.g.http://www.website.com/api/whoamiwould be forwarded tohttp://api.website.com/whoami, butapi.website.comwould set cookies with the path param not prefixed with/api.Solved with the following code (reads
set-cookiefrom incoming target response and prefixes cookie path params with/api:Would be nice if this was a configurable option for
http-proxy-middlewaresince it is probably a fairly common scenario.