5

I'm trying to proxy calls to a REST API using http-proxy, but it keeps returning 404 codes.

This is an example call to my API: http://petrpavlik.apiary-mock.com/notes It returns some JSON data.

This is what my server code looks like:

var httpProxy = require('http-proxy');

var proxy = httpProxy.createServer({
  target:'http://petrpavlik.apiary-mock.com'
});

proxy.listen(8005);

proxy.on('error', function (err, req, res) {
  res.writeHead(500, {
    'Content-Type': 'text/plain'
  });

  res.end('Something went wrong. And we are reporting a custom error message.');
});

proxy.on('proxyRes', function (res) {
  console.log('RAW Response from the target', JSON.stringify(res.headers, true, 2));
});

This is what I get when I try to call the same request using my proxy.

Petrs-MacBook-Air-2:~ petr$ curl -v 127.0.0.1:8005/notes
* About to connect() to 127.0.0.1 port 8005 (#0)
*   Trying 127.0.0.1...
* Adding handle: conn: 0x7f977280fe00
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7f977280fe00) send_pipe: 1, recv_pipe: 0
* Connected to 127.0.0.1 (127.0.0.1) port 8005 (#0)
> GET /notes HTTP/1.1
> User-Agent: curl/7.30.0
> Host: 127.0.0.1:8005
> Accept: */*
> 
< HTTP/1.1 404 Not Found
< cache-control: no-cache, no-store
< content-type: text/html; charset=utf-8
< date: Sat, 28 Jun 2014 09:40:29 GMT
* Server MochiWeb/1.0 (Any of you quaids got a smint?) is not blacklisted
< server: MochiWeb/1.0 (Any of you quaids got a smint?)
< content-length: 2960
< connection: Close
< 

I must be missing something obvious, but I'm really stuck on this one.

Petr Pavlík
  • 178
  • 1
  • 10

2 Answers2

13

If you are trying to proxy the Apiary Mock API, I have done it this way:

var apiProxy = httpProxy.createProxyServer();
server.get("/api/v1", function (req, res) {
  delete req.headers.host;
  req.url = req.url.replace('/api/v1', '/');
  apiProxy.web(req, res, { target: 'http://private-XXXX.apiary-mock.com/' });
});

Now in my local app I request GET /api/v1/notes being proxied to http://private-XXXX.apiary-mock.com/notes.

Make sure you delete req.headers.host, otherwise the target server would match the wrong host.

gpbl
  • 4,721
  • 5
  • 31
  • 45
  • 1
    Thanks for ``delete req.headers.host;``. The same issue impacted me with proxying Google APIs. – user85461 Feb 17 '15 at 00:23
  • Where do you initialize "server"? – Tarion Oct 12 '15 at 11:01
  • Stumbled into `Page Not Found Looks like you've followed a broken link or entered a URL that doesn't exist on this site.` for a while. This works for me. – Scott Chang Jun 30 '21 at 06:27
  • is there any option to do it automatically? – redexp Aug 11 '22 at 12:34
  • 1
    This 9-year-old answer got me out of my problem! I ran nextjs locally during development and proxying/forwarding requests to a remote API running behind an NGINX ingress on a k8s cluster. I was getting `404` responses from NGINX. After deleting the host header from `req`, I get `200`'s. The ingress, of course, couldn't find the paths I was requesting a `localhost`. – Infiniteh May 02 '23 at 15:01
7

I stumbled into the same problem but the solution didn't work for me. Eventually this solved my 404 errors:

var proxyOptions = {
    ignorePath: true 
};

var apiProxy = httpProxy.createProxyServer(proxyOptions);
midbyte
  • 71
  • 1
  • 1