http-party / node-http-proxy

A full-featured http proxy for node.js
https://github.com/http-party/node-http-proxy
Other
13.88k stars 1.97k forks source link

How to get https API response body on proxyRes #1614

Open guvensen opened 1 year ago

guvensen commented 1 year ago

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;

function interceptLoginResponse(proxyRes, req, res) {
            // Read the API's response body from 
            let apiResponseBody = ''
            proxyRes.on('data', (chunk) => {
                chunk = chunk.toString('utf-8');
                apiResponseBody += chunk
            })

            proxyRes.on('end', async () => {
                try {
                    // Extract the authToken from API's response:
                    const {access_token} = await JSON.parse(apiResponseBody);

                    const cookies = new Cookies(req, res, {secure: true})

                    await cookies.set('access_token', access_token, { secure:true, pat:"/", httpOnly: true, sameSite: 'Strict', expirationDate:"21 Oct 2022 07:28:00 GMT"})
                    await res.status(200).json({ success: true })
                } catch (err) {
                    reject(err)
                }
            })
        }
MrDeerly commented 4 months ago

Have you ever found a way to accomplish this?

guvensen commented 2 months ago

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).
        })
    });
}