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.

Cannot proxy after parsing body

See original GitHub issue

I cannot re-send a proxied request’s body after I have parsed it.

Using this recipe, this comment and this comment I have written the following example:

import express from 'express';
import proxy from 'http-proxy-middleware';
import bodyParser from 'body-parser'

const app = express();
app.use(bodyParser.json());
app.post('*', proxy({
    target: 'http://target.here.com',
    secure: false,
    onProxyReq: (proxyReq, req) => {
        if (req.body) {
            const bodyData = JSON.stringify(req.body);
            proxyReq.setHeader('Content-Type','application/json');
            proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
            proxyReq.write(bodyData);
        }
    }
}));
app.listen(2001, () => console.log('Listening'));

I get an error when trying to set the first header on the proxyReq saying: Cannot set headers after they are sent to the client.

If that is the case how does the recipe work?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
SBRKcommented, Sep 26, 2018

+1 got the same issue. Moving the middlewares before body.json works

3reactions
windixcommented, Dec 13, 2018

If need to parse the body before proxy, I have followed suggestion from this issue: https://github.com/chimurai/http-proxy-middleware/issues/177#issuecomment-420423934

Read more comments on GitHub >

github_iconTop Results From Across the Web

Not getting response using an express proxy server when ...
I am facing a problem where I am not getting a request back when I use this in one of the servers :...
Read more >
Unable to parse response body - Opster
If your client is getting the response from the reverse proxy service this could be what is causing the “Unable to parse response...
Read more >
The message body is not a valid JSON. Unexpected character ...
ApiManagement. Proxy: The message body is not a valid JSON. Unexpected character encountered while parsing value. - Microsoft Q&A.
Read more >
RFC 7230: Hypertext Transfer Protocol (HTTP/1.1)
One consequence of this flexibility is that the protocol cannot be ... a header field-value after message parsing has delineated the individual fields....
Read more >
5.x API - Express.js
This parser accepts any Unicode encoding of the body and supports automatic ... Since router and app implement the middleware interface, you can...
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