177

Curl offers a series of different http method calls that are prefixed with a X, but also offers the same methods without. I've tried both and I can't seem to figure out the difference. Can someone explain to me quickly how these two operations differ?

lospejos
  • 1,976
  • 3
  • 19
  • 35
matsko
  • 21,895
  • 21
  • 102
  • 144

3 Answers3

343

By default you use curl without explicitly saying which request method to use. If you just pass in a HTTP URL like curl http://example.com it will use GET. If you use -d or -F curl will use POST, -I will cause a HEAD and -T will make it a PUT.

If for whatever reason you're not happy with these default choices that curl does for you, you can override those request methods by specifying -X [WHATEVER]. This way you can for example send a DELETE by doing curl -X DELETE [URL].

It is thus pointless to do curl -X GET [URL] as GET would be used anyway. In the same vein it is pointless to do curl -X POST -d data [URL]... But you can make a fun and somewhat rare request that sends a request-body in a GET request with something like curl -X GET -d data [URL].

Digging deeper

curl -GET (using a single dash) is just wrong for this purpose. That's the equivalent of specifying the -G, -E and -T options and that will do something completely different.

There's also a curl option called --get to not confuse matters with either. It is the long form of -G, which is used to convert data specified with -d into a GET request instead of a POST.

(I subsequently used my own answer here to populate the curl FAQ to cover this.)

Warnings

Modern versions of curl will inform users about this unnecessary and potentially harmful use of -X when verbose mode is enabled (-v) - to make users aware. Further explained and motivated in this blog post.

-G converts a POST + body to a GET + query

You can ask curl to convert a set of -d options and instead of sending them in the request body with POST, put them at the end of the URL's query string and issue a GET, with the use of `-G. Like this:

curl -d name=daniel -d grumpy=yes -G https://example.com/
Daniel Stenberg
  • 54,736
  • 17
  • 146
  • 222
  • 6
    -XGET may be no-op, but it does make it explicit. – mtyson Feb 26 '16 at 17:43
  • 1
    "By default you use curl without explicitly saying which request method to use. If you just pass in a HTTP URL like curl http://example.com it will use GET. If you use -d or -F curl will use POST, -I will cause a HEAD and -T will make it a PUT." Everything you need to know. – Daniel Viglione Mar 08 '18 at 18:24
  • 8
    Explicit is better than implicit whenever you have the option. Fail early, fail fast, reduces accidents, reduces debug time. The only time you have an excuse for skipping it is when typing it into the command line. Any script should specify -XGET even when strictly unnecessary. – Backgammon Feb 25 '19 at 19:54
  • 2
    @Backgammon if they do, they're doing it wrong. But sure, they can. – Daniel Stenberg Feb 25 '19 at 20:46
  • Great, I use the `-X` flag together with `patch` to do a github api [call](https://stackoverflow.com/a/6603754/1705829) – Timo Mar 08 '22 at 11:15
7

The use of -X [WHATEVER] merely changes the request's method string used in the HTTP request. This is easier to understand with two examples — one with -X [WHATEVER] and one without — and the associated HTTP request headers for each:

# curl -XPANTS -o nul -v http://neverssl.com/
* Connected to neverssl.com (13.224.86.126) port 80 (#0)
> PANTS / HTTP/1.1
> Host: neverssl.com
> User-Agent: curl/7.42.0
> Accept: */*

# curl -o nul -v http://neverssl.com/
* Connected to neverssl.com (13.33.50.167) port 80 (#0)
> GET / HTTP/1.1
> Host: neverssl.com
> User-Agent: curl/7.42.0
> Accept: */*
Jimadine
  • 998
  • 13
  • 26
4

-X [your method]
X lets you override the default 'Get'

** corrected lowercase x to uppercase X

user9074332
  • 2,336
  • 2
  • 23
  • 39
hoogw
  • 4,982
  • 1
  • 37
  • 33