{"id":16726,"date":"2021-05-14T23:39:04","date_gmt":"2021-05-14T21:39:04","guid":{"rendered":"https:\/\/daniel.haxx.se\/blog\/?p=16726"},"modified":"2021-05-16T23:11:51","modified_gmt":"2021-05-16T21:11:51","slug":"curl-g-vs-curl-x-get","status":"publish","type":"post","link":"https:\/\/daniel.haxx.se\/blog\/2021\/05\/14\/curl-g-vs-curl-x-get\/","title":{"rendered":"curl -G vs curl -X GET"},"content":{"rendered":"\n<p>(This is a repost of a <a href=\"https:\/\/stackoverflow.com\/a\/8502004\/93747\">stackoverflow answer I once wrote<\/a> on this topic. Slightly edited. Copied here to make sure I own and store my own content properly.)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">curl knows the HTTP method<\/h2>\n\n\n\n<p>You normally use curl without explicitly saying which request method to use.<\/p>\n\n\n\n<p>If you just pass in a HTTP URL like <code>curl http:\/\/example.com<\/code>, curl will use GET. If you use <code><a href=\"https:\/\/curl.se\/docs\/manpage.html#-d\">-d<\/a><\/code> or <code><a href=\"https:\/\/curl.se\/docs\/manpage.html#-F\">-F<\/a><\/code> curl will use POST, <code>-<a href=\"https:\/\/curl.se\/docs\/manpage.html#-I\">I<\/a><\/code> will cause a HEAD and <code><a href=\"https:\/\/curl.se\/docs\/manpage.html#-T\">-T<\/a><\/code> will make it a PUT.<\/p>\n\n\n\n<p>If for whatever reason you&#8217;re not happy with these default choices that curl does for you, you can override those request methods by specifying <code>-X [WHATEVER]<\/code>. This way you can for example send a DELETE by doing <code>curl -X DELETE [URL]<\/code>.<\/p>\n\n\n\n<p>It is thus pointless to do <code>curl -X GET [URL]<\/code> as GET would be used anyway. In the same vein it is pointless to do <code>curl -X POST -d data [URL]...<\/code> But you can make a fun and somewhat rare request that sends a request-body in a GET request with something like <code>curl -X GET -d data [URL]<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Digging deeper<\/h2>\n\n\n\n<p><code>curl -GET<\/code> (using a single dash) is just wrong for this purpose. That&#8217;s the equivalent of specifying the <a href=\"https:\/\/curl.se\/docs\/manpage.html#-G\"><code>-G<\/code><\/a>, <a href=\"https:\/\/curl.se\/docs\/manpage.html#-E\"><code>-E<\/code><\/a> and <a href=\"https:\/\/curl.se\/docs\/manpage.html#-T\"><code>-T<\/code><\/a> options and that will do something completely different.<\/p>\n\n\n\n<p>There&#8217;s also a curl option called <a href=\"https:\/\/curl.se\/docs\/manpage.html#-G\"><code>--get<\/code><\/a> to not confuse matters with either. It is the long form of -G, which is used to convert data specified with <a href=\"https:\/\/curl.se\/docs\/manpage.html#-d\"><code>-d<\/code><\/a> into a GET request instead of a POST.<\/p>\n\n\n\n<p>(I subsequently used this answer to populate the <a href=\"https:\/\/curl.se\/docs\/faq.html#curl_X_gives_me_HTTP_problems\">curl FAQ to cover this<\/a>.)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Warnings<\/h2>\n\n\n\n<p>Modern versions of curl will inform users about this unnecessary and potentially harmful use of -X when verbose mode is enabled (<code>-v<\/code>) &#8211; to make users aware. Further explained and motivated <a href=\"https:\/\/daniel.haxx.se\/blog\/2015\/09\/11\/unnecessary-use-of-curl-x\/\">here<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">-G converts a POST + body to a GET + query<\/h2>\n\n\n\n<p>You can ask curl to convert a set of <code>-d<\/code> options and instead of sending them in the request body with POST, put them at the end of the URL&#8217;s query string and issue a GET, with the use of `-G. Like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">curl -d name=daniel -d grumpy=yes -G https:\/\/example.com\/<\/pre>\n\n\n\n<p>&#8230; which does the exact same thing as this command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">curl https:\/\/example.com\/?name=daniel&amp;grumpy=yes<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>(This is a repost of a stackoverflow answer I once wrote on this topic. Slightly edited. Copied here to make sure I own and store my own content properly.) curl knows the HTTP method You normally use curl without explicitly saying which request method to use. If you just pass in a HTTP URL like &hellip; <a href=\"https:\/\/daniel.haxx.se\/blog\/2021\/05\/14\/curl-g-vs-curl-x-get\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">curl -G vs curl -X GET<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":5,"featured_media":14034,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[33,230,452],"class_list":["post-16726","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-curl","tag-curl-and-libcurl","tag-http","tag-stackoverflow"],"_links":{"self":[{"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/posts\/16726","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/comments?post=16726"}],"version-history":[{"count":11,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/posts\/16726\/revisions"}],"predecessor-version":[{"id":16743,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/posts\/16726\/revisions\/16743"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/media\/14034"}],"wp:attachment":[{"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/media?parent=16726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/categories?post=16726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/tags?post=16726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}