Skip to content

HttpProxyMiddleware [ERR_STREAM_WRITE_AFTER_END] when using agent #1021

@twinkle77

Description

@twinkle77

Checks

Describe the bug (be clear and concise)

I'm using HttpProxyMiddleware, with those options:

export const httpProxy = createProxyMiddleware({
  agent: new HttpsProxyAgent('http://127.0.01:10701'),
  pathRewrite: {
    '/tob_gateway': '',
  },
  router: () => {
    return 'http://URL_ADDRESSxxxxx';
  },
  secure: false,
  changeOrigin: true,
  on: {
    proxyReq: (proxyReq, req) => {
      const requestBody = (req as unknown as BodyParserLikeRequest)?.body;

      if (!requestBody) {
        return;
      }

      const writeBody = (bodyData: string) => {
        proxyReq.write(bodyData);
      };

      const contentType = proxyReq.getHeader('Content-Type') as string;
      if (
        contentType &&
        (contentType.includes('application/json') ||
          contentType.includes('+json'))
      ) {
        writeBody(JSON.stringify(requestBody));
      }

      if (
        contentType &&
        contentType.includes('application/x-www-form-urlencoded')
      ) {
        writeBody(querystring.stringify(requestBody));
      }
    },
  },
});

I know that the body is empty since I read the stream with express.raw which using bodyParser behind the scenes, but how can I write it back again before its sent? I cant make any request changes on 'onProxyReq', since I get errors like the above, and if I try to change any headers I get [ERR_HTTP_HEADERS_SENT]

I want to write the request body back before I send it to the server

Step-by-step reproduction instructions

1. ...
2. ...

Expected behavior (be clear and concise)

i can to write body when proxyReq event is triggered( use agent option)

How is http-proxy-middleware used in your project?

as a http proxy

What http-proxy-middleware configuration are you using?

export const httpProxy = createProxyMiddleware({
  agent: new HttpsProxyAgent('http://127.0.01:10701'),
  pathRewrite: {
    '/tob_gateway': '',
  },
  router: () => {
    return 'http://URL_ADDRESSxxxxx';
  },
  secure: false,
  changeOrigin: true,
  on: {
    proxyReq: (proxyReq, req) => {
      const requestBody = (req as unknown as BodyParserLikeRequest)?.body;

      if (!requestBody) {
        return;
      }

      const writeBody = (bodyData: string) => {
        proxyReq.write(bodyData);
      };

      const contentType = proxyReq.getHeader('Content-Type') as string;
      if (
        contentType &&
        (contentType.includes('application/json') ||
          contentType.includes('+json'))
      ) {
        writeBody(JSON.stringify(requestBody));
      }

      if (
        contentType &&
        contentType.includes('application/x-www-form-urlencoded')
      ) {
        writeBody(querystring.stringify(requestBody));
      }
    },
  },
});

What OS/version and node/version are you seeing the problem?

mac: 14.4.1
node: v20.15.0

Additional context (optional)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions