37

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;
    }
  )
}
Kyle Krzeski
  • 6,183
  • 6
  • 41
  • 52
  • 1
    Just send an empty object – Antikhippe Nov 22 '17 at 18:45
  • very good answer @Antikhippe. you have to add a `body` even if it is empty , undefined etc. – DrNio Nov 22 '17 at 18:48
  • 12
    No, that's actually not a good answer. Passing an empty object will not post a request without body. It will post a request with a body containing an empty JSON object (`{}`). Pass null. – JB Nizet Nov 22 '17 at 18:51
  • What are you trying to do with this Request though? What are you expecting in the backend from POSTing to this `/api?data=` route? – Chau Tran Nov 22 '17 at 18:56
  • @Jota.Toledo Please see [THIS](https://stackoverflow.com/questions/4191593/is-it-considered-bad-practice-to-perform-http-post-without-entity-body) question as mentioned in my original post. – Kyle Krzeski Nov 22 '17 at 19:25
  • My bad, oversaw that resource. Thanks for the heads up! – Jota.Toledo Nov 22 '17 at 19:26
  • I had a similar problem and my issue was I didn't subscribe to the observable. The POST was meant as a command so nothing was returned. Still need to subscribe to get it to execute. – hkrauss2 Jan 18 '19 at 22:03
  • Use .subscribe(); Found the answer in https://stackoverflow.com/questions/65077714/angular-sending-post-request-with-empty-request-body – wast Jul 11 '22 at 10:30

6 Answers6

51

Looks like this is the appropriate answer:

postRequest(id) {
  this.http.post('/api?data=' + id, null).map(
    (response) => {
      return response;
    }
  )
}
Kyle Krzeski
  • 6,183
  • 6
  • 41
  • 52
4

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

postRequest(id) {
    this.http.post('/api?data=' + id, {}).map((response) => {return response;});
}
ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
Satish Patro
  • 3,645
  • 2
  • 27
  • 53
  • null may not solve. I tried it and it does not solve. And, I think we need to send JSON anyhow. So, empty JSON file it is – Satish Patro Jan 24 '19 at 08:30
3

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[];
    };
1

Try to call HTTP 'Post' without passing a 'body', and got this error in Angular 6 -

enter image description here

If we go to definition of POST method in client.d.ts, it shows that post method required either 'body' or 'null' value (e.g. - body: any | null)-

post(url: string, body: any | null, options?: {
...
}): Observable<Object>;

Solution - I just added 'null' and the error gone -

postSync(): any {
    return this.http.post(`${environment.apiUrl}/xyz/sync-api`, null);
}
Pinaki
  • 792
  • 8
  • 17
0

If request body is not there for post or put method then send empty object to request. ex: {}.

0

Cannot comment the accepted answer, so I'll just add that passing null is not sufficient, because the content type will not be inferred correctly and you may receive a 415 (at least that was my case). Use the options on http.post to specify the content type in the header and force json with the null body

this.http.post<T>(url, null, {
 headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json'
 }
});
flop
  • 1