Open guvensen opened 1 year ago
Have you ever found a way to accomplish this?
Hi @MrDeerly
Very long time ago I solved it. You can review the code below.
import httpProxy from 'http-proxy'
const API_URL = process.env.API_URL;
export const config = {
api: {
bodyParser: false
}
}
/**
* TEST http-proxy
*/
const proxy = httpProxy.createProxyServer({})
export default (req, res) => {
return new Promise((resolve, reject) => {
// Get the `auth-token` cookie:
req.url = req.url.replace(/^\/api\/proxy/, '');
// Don't forward cookies to the API:
req.headers.cookie = ''
// Don't forget to handle errors:
proxy.once('error', reject)
proxy.once('proxyReq', (proxyReq, req, res)=>{
try {
proxyReq.setHeader('Authorization','Bearer ' + process.env.API_TOKEN);
} catch (err) {
reject(err)
}
})
// change request before it's being sent to target
delete req.headers.origin;
// Forward the request to the API
proxy.web(req, res, {
target: API_URL,
// Don't autoRewrite because we manually rewrite
// the URL in the route handler.
autoRewrite: false,
changeOrigin: true,
// In case we're dealing with a login request,
// we need to tell http-proxy that we'll handle
// the client-response ourselves (since we don't
// want to pass along the auth token).
})
});
}
Hello all!
I have an http and an https API.
When I use http API, when user login successfully I get access token on proxyRes and I add HttpOnly Cookie. This is working.
But when I use https API I can't get response body. In my opinion because the response body is encrypted.
How can I solution this problem. Is there a solution?
This my code snippet;