user3154990 user3154990 - 1 year ago 158
reST (reStructuredText) Question

Angular 2 http post is returning 200 but no response is returned

My http call is returning 200 but no response is captured. My code inside subscribe is not being hit. The API is returning data when I test in postman. Here is my code.

getToken(authcode: string) {

var data = 'client_id=InspectWebApp_client&code=' + authcode + '&redirect_uri=http://localhost:3000&grant_type=authorization_code';
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
let options = new RequestOptions({ headers: headers });'', data, options)
.subscribe((res: Response) => {

var resultsToken = res.json();
//return this.inspections;


Answer Source

I was also facing the same problem. The problem was solved using the map function on Observables. Here is my implementation:

login(Username:string, Password:string) : Observable<Response>{
    let headers = new Headers();
    headers.append("Authorization", "Basic " + btoa(Username + ":" + Password)); 
    headers.append("Content-Type", "application/x-www-form-urlencoded");
    return"auth/login", " " , {headers: headers}  )
        .map((response: Response) => {
            return response;     

Here the handleError is a function to catch the excceptions generated. This is a function in login.service.ts that sends the username and password to the api to get data. You can see that I am returning response from the map function in this service. Now, this returned response can be caught in subscribe function in following way:

this._loginService.login(this.username, this.password)  
            (response) => {
                //Here you can map the response to a type.
                this.apiResult = <IUser>response.json();
            (err) => {
                //Here you can catch the error
            () => {this.router.navigate(['home'])}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download