0

I am trying to add a custom header to a proxy request when using ng serve in my Angular 18 app. I've tried the following configurations:

// proxy.config.js
const proxyConfig = {
  "/api/v1": {
    target: "http://localhost:3000",
    secure: false,
    changeOrigin: true,
    onProxyReq: (proxyReq) => {
      proxyReq.setHeader("X-foo", "bar");
    },
  },
};
module.exports = proxyConfig;

And this alternative syntax:

// proxy.config.js
const proxyConfig = {
  "/api/v1": {
    target: "http://localhost:3000",
    secure: false,
    changeOrigin: true,
    on: {
      proxyReq: (proxyReq) => {
        proxyReq.setHeader("X-foo", "bar");
      },
    },
  },
};
module.exports = proxyConfig;

Neither of these work. The custom X-foo header is not added to the request and I don't see any errors in the console.

What am I doing wrong?

1 Answer 1

0

In Angular 18 or later, you have to use a different syntax:

// proxy.config.js
const proxyConfig = {
  "/api/v1": {
    target: "http://localhost:3000",
    secure: false,
    changeOrigin: true,
    configure(proxy) {
      proxy.on("proxyReq", (proxyReq) => {
        proxyReq.setHeader("X-foo", "bar");
      });
    },
  },
};
module.exports = proxyConfig;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.