Proxy configuration is not working in angular 6 Proxy configuration is not working in angular 6 angular angular

Proxy configuration is not working in angular 6


I had this same issue and adding proxyConfig option to the serve target did the job.

Go to angular.json -> within serve add the key:value pair "proxyConfig": "proxy.conf.json"

like this -

"architect": {  "serve": {    "builder": "@angular-devkit/build-angular:dev-server",    "options": {      "browserTarget": "your-application-name:build",      "proxyConfig": "proxy.conf.json"    },

btw, I got this from here


It took me a couple of hours to fix incorrect routing of POST /api/auth/login to https://localhost:5001 which is in the image you provided.

Following config fixes the routing of POST /api/auth/login to https://localhost:5001/api/auth/login.

{    "/api": {        "target": "https://localhost:5001/api",        "changeOrigin": true,        "logLevel": "debug",        "pathRewrite": { "^/api" : "" }    }}


You said you are new to this, so let me explain something simple first.

When you call http.post the first parameter is the url that will be contacted. So:

this.http.post(this.baseUrl + 'login', ...

will become

this.http.post('api/auth/login', ...

because you set baseUrl and added 'login':

baseUrl = 'api/auth/';

But your code does not say which protocol to use (http or https) NOR which domain:port to call (eg. http://mydomain:port/api/auth/login).

Therefore Angular will default to 'http' and to the domain and port that IT is using, which is localhost:4200. So your request then becomes:

http://localhost:4200/api/auth/login

That is why you see that in the console. Angular is completely ignoring your proxy file. I think the reason is either the name, or location (it should normally be in your project's root folder) or you have not told Angular to load it.

You need to tell Angular to actually use it when it starts up. So in package.json you need something like this:

"start": "ng serve --proxy-config proxyconfig.json"

This tells Angular-CLI that when you use the npm run start (or just npm start) command to start the app, it should load the proxy data from that json file in the root folder.

I would advise reading this tutorial also.

Also, I think you have /* missing from your proxyconfig:

{    "/api/*": {        "target": "http://localhost:5001",        "secure": false,        "changeOrigin": true,        "logLevel": "debug"    }}