title: Unable to do post request with HttpClient in angular using proxy config with solution
body: <p>While I am trying to make post request in <code>HttpClient</code> getting the below error while same with rest client giving proper response. Same with angular 1 <code>$http</code> service is working as expected.</p>
  
  <p>Tried multiple ways but neither post nor get method is working.
  I am using angular-cli in which I have configured <code>proxy.config.json</code></p>
  
  <pre><code>{
      "/api/*":{
  
          "target":"http://10.104.40.14:8290/my_app",
          "secure":false,
          "logLevel":"debug"
      }
  }
  </code></pre>
  
  <p>// error code</p>
  
  <pre><code> zone.js:2933 POST http://localhost:4200/api/security/login 401 (Unauthorized)
       &lt;html&gt;
      &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html;charset=utf-8"/&gt;
      &lt;title&gt;Error 401 Unauthorized&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;&lt;h2&gt;HTTP ERROR 401&lt;/h2&gt;
      &lt;p&gt;Problem accessing /de_prp/error. Reason:
      &lt;pre&gt;    Unauthorized&lt;/pre&gt;&lt;/p&gt;
      &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>
  
  <p>Here is my auth.service.ts file</p>
  
  <pre><code>    import { Injectable, OnInit } from '@angular/core';
  
     import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
          import { RequestOptions, Headers } from '@angular/http';
          import { URLSearchParams } from '@angular/http';
          @Injectable()
          export class AuthService implements OnInit {
            constructor(private http: HttpClient) { }
  
            ngOnInit(): void {
  
            }
  
  
            login(username, password, rememberMe) {
              console.log(username, password, rememberMe);
  
              //const headers = new HttpHeaders().set('Content-Type', 'application/json; charset=utf-8');
              const body = JSON.stringify({ username: username, password: password });
              const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
              this.http.post("/api/security/login", body, { headers: headers }).subscribe(
                res =&gt; {
                  console.log(res);
                },
                (err: HttpErrorResponse) =&gt; {
  
                  console.log(err.error);
                  console.log(err.name);
                  console.log(err.message);
                  console.log(err.status);
                }
              )
            }
            logout() {
  
              this.http.get("/api/auth/logout").subscribe(
                res =&gt; {
                  console.log(res);
                },
                (err: HttpErrorResponse) =&gt; {
  
                  console.log(err.error);
                  console.log(err.name);
                  console.log(err.message);
                  console.log(err.status);
                }
              );
            }
          }
  </code></pre>
  
  <p>Solution: Finally resolve by entry in proxy.config.json given below
  "pathRewrite": {"^/api" : ""} so final json file is </p>
  
  <pre><code>{
      "/api/*":{
  
          "target":"http://10.104.40.14:8290/my_app",
          "secure":false,
          "pathRewrite": {"^/api" : ""}
      }
  }
  </code></pre>