Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a full response in Angular http Post request

Tags:

angular

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"'.
like image 250
Noober Avatar asked Jun 22 '18 14:06

Noober


1 Answers

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) ...
like image 188
Witold Kaczurba Avatar answered Sep 18 '22 14:09

Witold Kaczurba