Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Post with no body in Angular

I'm wondering how to send an HTTP post request without a body (specifically in Angular). Here's what I'm doing now, but I'm getting the error Expected 2-3 arguments, but got 1).

I realize the second argument is for the body, but I'm not sending that to the server (yes, I understand that a POST call changes the state of a system and have looked into THIS question).

postRequest(id) {
  this.http.post('/api?data=' + id).map(
    (response) => {
      return response;
    }
  )
}
like image 485
Kyle Krzeski Avatar asked Nov 22 '17 18:11

Kyle Krzeski


3 Answers

Looks like this is the appropriate answer:

postRequest(id) {
  this.http.post('/api?data=' + id, null).map(
    (response) => {
      return response;
    }
  )
}
like image 194
Kyle Krzeski Avatar answered Oct 07 '22 18:10

Kyle Krzeski


Go to definition of POST method using your IDE and you can see passing either body: any | null are the available inputs

post(url: string, body: any | null, options: {
    headers?: HttpHeaders | {
        [header: string]: string | string[];
    };
like image 41
Kevin Brennan Avatar answered Oct 07 '22 18:10

Kevin Brennan


If null is not working (4XX error client side), try with {} JSON:

postRequest(id) {
    this.http.post('/api?data=' + id, {}).map((response) => {return response;});
}
like image 3
Satish Patro Avatar answered Oct 07 '22 17:10

Satish Patro