Headers not sent with the request through a proxy server

40 Views Asked by At
import { Request, Response, NextFunction } from 'express';
import axios from 'axios';

import service_ip_map from '../ip_addr';

export default async function escort_controller(req: Request, res: Response, _next: NextFunction) {
  const headers = { service_name: req.params.service_name, ...req.headers };

  const target_service = {
    ip: service_ip_map[req.params.service_name],
    method: req.method,
    body: req.body,
    headers,
    route: req.url,
    // url: req.originalUrl.split('/')[req.originalUrl.split('/').length - 1],
  };

  try {
    const response = await axios({
      method: `${target_service.method}`,
      url: `http://${target_service.ip}${target_service.route}`,
      headers,
      data: target_service.body,
    });

    res.send({ data: response.data });
  } catch (error) {
    throw new Error(error);
  }
}

In the above code I am trying to create a proxy server where I am capturing req.headers and passing it down through axios to another service. The issue is that the request just hangs and doesn't resolve (through postman).

But if I add custom headers like in the below example it works fine.

const target_service = {
    ip: service_ip_map[req.params.service_name],
    method: req.method,
    body: req.body,
    headers: {
      test_header: 'testing'
    },
    route: req.url,
    // url: req.originalUrl.split('/')[req.originalUrl.split('/').length - 1],
  };

Also When I make the same request through the browser and add some headers (be it from. req.headers or any custom headers for testing purposes those headers also don't show up in the browser, but the request doesn't hang up or fail in any way.

I tried to shallow copy the req.headers but that didn't work.

0

There are 0 best solutions below