Getting a full response in Angular http Post request

I am trying to get full response from a POST request. I have read how to get full response for a get request mentioned at the official site of angular. Angular http

What it says is to add { observe: 'response' }. But it would work for a get request and not for post request. post request accepts 2-3 arguments, so I cannot send this as the 4th argument. Please have a look at my code and let me know what I am doing wrong.

    const httpOptions = {
        headers: new HttpHeaders({
          'Content-Type':  'application/json'
        })
    };

    return this.http.post('http://localhost:3000/api/Users/login', data, httpOptions, { observe: 'response' })
        .do( function(resp) {
            self.setSession(resp);
        });

This gives me an error as 4 arguments are not allowed.

Edit

The accepted answer seems to be not working now. I am getting the following

error:
error TS2345: Argument of type '{ headers: HttpHeaders; observe: string; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.
  Types of property 'observe' are incompatible.
    Type 'string' is not assignable to type '"body"'.

observe should be part of the httpOptions as a property.

const httpOptions = {
    headers: new HttpHeaders({
      'Content-Type':  'application/json'
    }),
    observe: 'response'
};

 return this.http.post('http://localhost:3000/api/Users/login', data, httpOptions)
   .do( function(resp) {
        self.setSession(resp);
 });

We use HttpClient to send a GET request to our API server to retrieve the first page of data. The get () method takes the URL of API endpoint as a first parameter and an options object as the second parameter. In the options object, we add the observe property with a response value to instruct Angular to provide us with the full HTTP response.

So: I needed to declare more explicitly type of httpOptions:

const httpOptions: { headers; observe; } = {
  headers: new HttpHeaders({
    'Content-Type':  'application/json'
  }),
  observe: 'response'
};


return this.http.post('http://localhost:3000/api/Users/login', data, httpOptions) ...

Simple GET request with response type <any> This sends an HTTP GET request to the npm api for a list of packages that belong to the @angular scope, then assigns the total returned in the response to the local property totalAngularPackages. The response type is set to <any> so it handle any properties returned in the response.

Set the options as following:

const httpOptions = {
    headers: new HttpHeaders({
      'Content-Type':  'application/json'
    }),
    observe: 'response' as 'response'
};

Casting 'response' as 'response' will force picking the appropriate overload.

Followings are the perfect steps to make an HTTP POST request in angular8 js. In this angular 8 HTTP POST Request tutorial, we will use the default app component. Let’s start with code examples. Import HttpClientModule in your app.module.ts file

I am updating my application from MEANAngular4 to MEANAngular6 but still don't know how to do http post/get requests. Thats how I did it in Angular 4 registerUser(user) { return this.http.post

In Http.post() method, we need to pass server URL, any object to post and request option that is optional. In request option we can set request headers such as content type and to handle this angular provides Headers and RequestOptions API.

In this how-to tutorial, we'll learn what is the HttpClient API available from HttpClientModule and how to use it in Angular 9 by example with request timeout, responsetype, query parameters, http headers, custom http headers, interceptors, typed and full responses, and error handling.

Comments
  • Yup, per documentation. angular.io/api/common/http/HttpClient#post
  • Hi, please have a look at my edit. The answer seems to be not working.
  • Not working anymore (Angular 9) => Types of property 'observe' are incompatible. Type 'string' is not assignable to type '"body"'.