Sepcio
Sepcio

Reputation: 255

Angular Proxy Configuration

I have a basic question but I cannot sort this out. I want to connect to git hub api from my angular app but always received 400 (Bad Request).

What I did:

  1. package.json - I added/edited "start": "ng serve --proxy-config proxy.config.json"
  2. I created new file proxy.config.json
  3. Inside this file I added below /api configuration

    { "/api": { "target": "https://api.github.com/", "secure": false } }

  4. In my someComponent.ts component I added:

    • import { HttpClient } from '@angular/common/http';
    • constructor(private http:HttpClient){}
    • and below func:

    somefunc(){ return this.http.get("/api/users?per_page=10") .subscribe(data =>{ this.datas = data console.log(this.datas) }) }

But when I tried to execute this get method in console I received:

GET http://localhost:4200/api/users?per_page=10 400 (Bad Request)

and

ERROR HttpErrorResponse {headers: HttpHeaders, status: 400, statusText: "Bad Request", url: "http://localhost:4200/api/users?per_page=10", ok: false, …}

Can someone help me what I did wrong, please?

Upvotes: 0

Views: 587

Answers (1)

Sepcio
Sepcio

Reputation: 255

For those who will have similar issue. If your api is not hosted on localhost (external api as on my example) you need to add below lines in proxy.config.json

Code to add:

"pathRewrite": { "^/api": "" },

Upvotes: 0

Related Questions