如何在 h2o2 代理中添加一个值以请求 header?
How to add a value to request header in h2o2 proxy?
hapi: 16.6.2, h2o2: 5.2.0
我有 h2o2 proxy 使用外部路由 API
{
method: 'GET',
path: '/api/v3/{param*}',
handler: {
proxy: {
host: 'host.net',
port: 8100,
protocol: 'http',
}
}
}
此 API 使用 client-side session。要维护 session,cookie 值应位于请求 header 内,例如:
Cookie: SESSION=352abdc5-c0de-49e3-985e-e07c7ab26a88
如何在 h2o2 中添加 cookie 值?
更新。
onRequest 应该可以访问上游请求。但是 Node.js 抛出错误
ValidationError: Invalid proxy handler options (/api/v3/{param*}) {
"host": "host.net",
"port": 8100,
"protocol": "http",
"onRequest" [1]: function onRequest(req) {\n console.log(req);\n }
}
[1] "onRequest" is not allowed
mapUri方法可以拦截上游请求
这里是 h2o2 代理路由的例子,其中一个 cookie 添加到请求中 headers:
const host = 'host.net';
const port = 8100;
const protocol = 'http';
const path = '/api/v3/{param*}';
const authCookie = 'SESSION=352abdc5-c0de-49e3-985e-e07c7ab26a88';
export default [
{
method: 'GET',
path,
handler: {
proxy: {
mapUri: function(req, cb) {
let {path, headers} = req;
const url = `${protocol}://${host}:${port}${path}`;
headers.cookie = authCookie;
return cb(null, url, headers);
},
onResponse: function(err, res, req, reply) {
return reply(res);
},
},
},
},
];
hapi: 16.6.2, h2o2: 5.2.0
我有 h2o2 proxy 使用外部路由 API
{
method: 'GET',
path: '/api/v3/{param*}',
handler: {
proxy: {
host: 'host.net',
port: 8100,
protocol: 'http',
}
}
}
此 API 使用 client-side session。要维护 session,cookie 值应位于请求 header 内,例如:
Cookie: SESSION=352abdc5-c0de-49e3-985e-e07c7ab26a88
如何在 h2o2 中添加 cookie 值?
更新。
onRequest 应该可以访问上游请求。但是 Node.js 抛出错误
ValidationError: Invalid proxy handler options (/api/v3/{param*}) {
"host": "host.net",
"port": 8100,
"protocol": "http",
"onRequest" [1]: function onRequest(req) {\n console.log(req);\n }
}
[1] "onRequest" is not allowed
mapUri方法可以拦截上游请求
这里是 h2o2 代理路由的例子,其中一个 cookie 添加到请求中 headers:
const host = 'host.net';
const port = 8100;
const protocol = 'http';
const path = '/api/v3/{param*}';
const authCookie = 'SESSION=352abdc5-c0de-49e3-985e-e07c7ab26a88';
export default [
{
method: 'GET',
path,
handler: {
proxy: {
mapUri: function(req, cb) {
let {path, headers} = req;
const url = `${protocol}://${host}:${port}${path}`;
headers.cookie = authCookie;
return cb(null, url, headers);
},
onResponse: function(err, res, req, reply) {
return reply(res);
},
},
},
},
];