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.

onProxyReq not fired

See original GitHub issue

Is this a bug report?

Yes.

Expected behavior

onProxyReq runs, allowing me to modify the request.

Actual behavior

onProxyReq is not run when a body is included in the request message, when C#'s HttpClient is used to issue the request.

The setup here is a little non-standard. I have a C# client that uses HttpClient. It makes a request which goes to a node server running http-proxy-middleware.

HttpClient is adding an Expect header for some reason – something it is entitled to do, but which browser don’t do. Usually, an Expect header indicates that we’re doing a 100-continue type hand signal. Microsoft’s implementation of HttpClient is presumably correct. Developers are not supposed to be explicitly modifying the Expect header.

When I issue a POST with no body, onProxyReq is fired. But when I issue a POST with a body, onProxyReq is never fired. I traced this to one of the latest commits in node-http-proxylink. The problem is fixed if that change is undone.

I don’t see any reason why they changed this upstream.

It’s not clear to me what actions I can take to make http-proxy-middleware proxy my requests that have bodies. I can’t modify the implementation or usage of HttpClient, only the server.

I have sizable headers, but the request body itself can be as short as “{}” in order to trigger this bug.

Setup

  • http-proxy-middleware: 1.0.6
  • http-proxy-middleware configuration
app.use(`/mypath`, createProxyMiddleware({

  target: 'http://foobar.com',
  changeOrigin: true,
  headers: {
    'X-Custom-Header': 'foobar'
  },
  preserveHeaderKeyCase: true,
  logLevel: 'debug'
  onProxyReq(proxyReq, req, res) {
    console.log('onProxyReq was fired');
  }
}));
  • server: express 4.17.1

client info

.NET 4.7.2’s HttpClient

target server info

Target agnostic – I’ve pointed it at an express server that just prints out information about requests that it receives.

Reproducible Demo

No demo at the moment.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sberneycommented, Feb 15, 2021

I forget how it’s related to this precise issue, and exactly what that codebase contained, but I did get my proxy server running. I had to check for the Expect header and strip it; use body-parser in the raw mode to then later copy the body onto the proxied request. I also had to run node 14, and start the server using the extra options argument to http(s).createServer(options, app), specifying the node-14-only max-header size property to be much larger.

I seem to have moved past this/worked around it, but I really don’t remember more details; I apologize.

Edit: I remember also possibly specifying older versions of the libraries involved, but not if that was included in the final solution.

1reaction
alwint3rcommented, Feb 15, 2021

I know it’s been months. But there’s a workaround here https://github.com/http-party/node-http-proxy/issues/1219, but I’m not sure if it’s the right solution. Regardless, is there any way for us to access the http-proxy directly from HPM?

Read more comments on GitHub >

github_iconTop Results From Across the Web

node-http-proxy proxy.on('proxyReq'...) never fires
I am using node.js proxy. It works successfully, however: proxy.on('proxyResponse', function (proxyRes, req, res) ...
Read more >
POST request body is not proxied to the servers · Issue #40 ...
Simple GET proxy does not work when the request url ends in .php (Using the vue-cli) #235 ... onProxyReq not fired #483.
Read more >
DevServer - webpack
This option allows specifying URL to web socket server (useful when you're proxying dev server and client script does not always know where...
Read more >
http-proxy-middleware - npm
Note: In multiple path matching, you cannot use string paths and ... onProxyReq: function, subscribe to http-proxy's proxyReq event.
Read more >
http-proxy-middleware createProxyMiddleware TypeScript ...
serverPassword}`, // Does not work to get auth set against backend proxyTimeout: PROXY_TIMEOUT, onProxyReq: (proxyReq: http.ClientRequest, req: http.
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