Angular 2 http.post() is not sending the request

AngularAngular2 Http

Angular Problem Overview


When I make a post request the angular 2 http is not sending this request

this.http.post(this.adminUsersControllerRoute, JSON.stringify(user), this.getRequestOptions())

the http post is not sent to the server but if I make the request like this

this.http.post(this.adminUsersControllerRoute, JSON.stringify(user), this.getRequestOptions()).subscribe(r=>{});

Is this intended and if it is can someone explain me why ? Or it is a bug ?

Angular Solutions


Solution 1 - Angular

Since the post method of the Http class returns an observable you need to subscribe it to execute its initialization processing. Observables are lazy.

You should have a look at this video for more details:

Solution 2 - Angular

You must subscribe to the returned observable if you want the call to execute.

See also the following angular documentation "Communicating with backend services using HTTP".

> ## Always subscribe! > An HttpClient method does not begin its HTTP request until you call subscribe() on the observable returned by that method. This is true for all HttpClient methods. > >> The AsyncPipe subscribes (and unsubscribes) for you automatically. > > All observables returned from HttpClient methods are cold by design. Execution of the HTTP request is deferred, allowing you to extend the observable with additional operations such as tap and catchError before anything actually happens. > > Calling subscribe(...) triggers execution of the observable and causes HttpClient to compose and send the HTTP request to the server. > > You can think of these observables as blueprints for actual HTTP requests. > >> In fact, each subscribe() initiates a separate, independent execution of the observable. Subscribing twice results in two HTTP requests. >> >> const req = http.get('/api/heroes'); >> // 0 requests made - .subscribe() not called. >> req.subscribe(); >> // 1 request made. >> req.subscribe(); >> // 2 requests made.

Solution 3 - Angular

Get method doesn't require to use the subscribe method but post method requires the subscribe. Get and post sample codes are below.

import { Component, OnInit } from '@angular/core'
import { Http, RequestOptions, Headers } from '@angular/http'
import 'rxjs/add/operator/map'
import 'rxjs/add/operator/catch'
import { Post } from './model/post'
import { Observable } from "rxjs/Observable";

@Component({
    templateUrl: './test.html',
    selector: 'test'
})
export class NgFor implements OnInit {

    posts: Observable<Post[]>
    model: Post = new Post()

    /**
     *
     */
    constructor(private http: Http) {
        
    }

    ngOnInit(){
        this.list()
    }

    private list(){
        this.posts = this.http.get("http://localhost:3000/posts").map((val, i) => <Post[]>val.json())
    }

    public addNewRecord(){
        let bodyString = JSON.stringify(this.model); // Stringify payload
        let headers      = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
        let options       = new RequestOptions({ headers: headers }); // Create a request option

        this.http.post("http://localhost:3000/posts", this.model, options) // ...using post request
                         .map(res => res.json()) // ...and calling .json() on the response to return data
                         .catch((error:any) => Observable.throw(error.json().error || 'Server error')) //...errors if
                         .subscribe();
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNicuView Question on Stackoverflow
Solution 1 - AngularThierry TemplierView Answer on Stackoverflow
Solution 2 - AngularIgorView Answer on Stackoverflow
Solution 3 - AngularmuratonerView Answer on Stackoverflow