68

Is there a way to use curl to send a POST request without sending any data?

We usually post like:

curl --data @C:\mydata.txt http://1.2.3.4/myapi

If you omit the --data you are doing a GET. How can you omit it and still do a POST?

Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
  • Warning, on Microsoft Windows, curl does not seem to work correctly under Power Shell, but does seem to work correctly under Command Prompt. – Eric Kolotyluk Apr 30 '18 at 20:12

5 Answers5

96

Randomly found the solution on another post:

curl -X POST http://example.com

Community
  • 1
  • 1
Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
  • Nice. I'm in this situation as well, but it feels a bit odd. I'm wondering if it would be more appropriate to change the call to a GET. I know this is a few years old by now, but any thoughts on that? – Kyle Clegg Jan 07 '14 at 19:27
  • 26
    In my case I had to use --data "" otherwise I would get a 411: curl -X POST --data "" url – unify Feb 12 '14 at 19:51
  • 11
    @KyleClegg The call should be a POST if it is designed to change something, and might give a different result every time it is called. Not sending data does not mean it won't change something. So in that case it would not be appropriate to change the call into a GET. – Michael L Mar 26 '15 at 01:07
16

Another option is sending a request with empty body, like so:

curl http://example.com -d {}
Afshin Mehrabani
  • 33,262
  • 29
  • 136
  • 201
3

This is a bit of a hack, but you could always provide an empty --data file.

Alternately

cat /dev/null | curl --data @- http://...
hemp
  • 5,602
  • 29
  • 43
1

In case of libcurl with PHP:

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
dog
  • 390
  • 2
  • 8
1

If you need to be authenticated then below is the command:

curl -X POST -u username:password http://example.com
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103