Reputation: 255
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:
package.json
- I added/edited "start": "ng serve --proxy-config proxy.config.json"
proxy.config.json
Inside this file I added below /api
configuration
{
"/api": {
"target": "https://api.github.com/",
"secure": false
}
}
In my someComponent.ts
component I added:
import { HttpClient } from '@angular/common/http';
constructor(private http:HttpClient){}
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
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