question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

http GET request is not proxied

See original GitHub issue

Expected behavior

GET request should be proxied properly

Actual behavior

GET request is totally ignored by the http-proxy-middleware while POST and DELETE request to the same target server work as expected

Setup

  • http-proxy-middleware: 0.17.4
  • server: browser-sync 2.18.11
  • other relevant modules

proxy middleware configuration

import config from '../webpack.config.dev';

const bundler = webpack(config);
const proxy = require('http-proxy-middleware');

const authenticationProxy = proxy('/authentication', {
    target: 'https://remote123.com/authentication-web/authentication',
    secure: false,
    pathRewrite: {
      '^/authentication' : ''
    }
});

const TACProxy = proxy('/terms-and-conditions', {
  target: 'https://remote456.com/info-web/terms-and-conditions',
  secure: false,
  pathRewrite: {
    '^/terms-and-conditions' : ''
  }
});

const infoProxy = proxy('/info', {
  target: 'https://remote456.com/info-web/info',
  secure: false
   pathRewrite: {
     '^/info' : ''
   }
});

server mounting

browserSync({
  port: 5200,
  ui: {
    port: 5201 // port where browsersync admin site will be avaialble
  },
  //cors: true,
  server: {
    baseDir: 'src', // base folder to serve

    middleware: [
      historyApiFallback(),

      webpackDevMiddleware(bundler, {
        publicPath: 'http://localhost:5200/',
        noInfo: true
      }),

      webpackHotMiddleware(bundler),

      // proxy for backend
      authenticationProxy,  //this works, POST method
      TACProxy, //this works, DELETE method
      infoProxy  // GET method NOT working, but POST works! same target host as the one above
    ]
  },

  files: [
    'src/*.html'
  ]
});

As the code snippet shown above, the TACProxy and infoProxy have same target host, remote456.com. If I send POST request to them, both proxies works; but GET request is always ignored by the proxy.

The response of GET request is suppose to be a json, but it’s actually the html that should be rendered by the browser. It’s like

<!DOCTYPE html><html lang="en"><head></head><body><div id="app"></div><script type="text/javascript" src="/main.bundle.js"></script></body></html>

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
b27lucommented, Jan 29, 2018

After a loooot of attempts, it suddenly works when I move my proxies settings in front of webpackDevMiddleware in the browserSync server mounting.

Like this,

browserSync({
  port: 5200,
  ui: {
    port: 5201 // port where browsersync admin site will be avaialble
  },
  //cors: true,
  server: {
    baseDir: 'src', // base folder to serve

    middleware: [
      historyApiFallback(),

      // IT WORKS AT HERE !!!
      authenticationProxy, 
      TACProxy, 
      infoProxy,

      webpackDevMiddleware(bundler, {
        publicPath: 'http://localhost:5200/',
        noInfo: true
      }),

      webpackHotMiddleware(bundler)

      // IT WAS AT HERE, BUT GET NOT WORKING
      // proxy for backend
      //authenticationProxy,  //this works, POST method
      //TACProxy, //this works, DELETE method
      //infoProxy  // GET method NOT working, but POST works! same target host as the one above
    ]
  },

  files: [
    'src/*.html'
  ]
});
1reaction
b27lucommented, Sep 5, 2017

I’ve also modified my '/info' api method call from a GET to a POST, and as soon as I do this (without any modification on the proxy configuration) it works (meaning the request is proxied as expected)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node http requests not going through proxy - Stack Overflow
This time Node attempted to send the request directly to the target server. This resulted in the proxy blocking the request completely. The...
Read more >
6.5. Tricky Things About Proxy Requests - HTTP - O'Reilly
Tricky Things About Proxy Requests This section explains some of the tricky and much misunderstood aspects of proxy server requests, including: How the...
Read more >
Make HTTP requests with the HttpClient - .NET - Microsoft Learn
To make an HTTP GET request, given an HttpClient and a URI, ... To specify that no proxy should be used, set the...
Read more >
HTTP/1.1: Header Field Definitions
If no Accept-Encoding field is present in a request, the server MAY assume that the client will accept any content coding. In this...
Read more >
How to proxy HTTP requests in Angular | by Maria Korneeva
Though the usual CORS error looks slightly different, the fact is — your backend does not like receiving requests from unmatching origins (which...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found