curl -GET 和 -X GET [英] curl -GET and -X GET
问题描述
Curl 提供了一系列不同的以 X 为前缀的 http 方法调用,但也提供了相同的方法.我都试过,我似乎无法弄清楚区别.有人可以快速向我解释这两种操作有何不同吗?
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?
推荐答案
默认情况下,您使用 curl 而不明确说明要使用哪种请求方法.如果你只是传入一个像 curl http://example.com
这样的 HTTP URL,它将使用 GET.如果您使用 -d
或 -F
curl 将使用 POST,-I
将导致 HEAD 和 -T
将使其成为 PUT.
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.
如果出于某种原因您对 curl 为您所做的这些默认选择不满意,您可以通过指定 -X [WHATEVER]
来覆盖这些请求方法.通过这种方式,您可以例如通过执行 curl -X DELETE [URL]
发送 DELETE.
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]
.
因此执行 curl -X GET [URL]
毫无意义,因为无论如何都会使用 GET.同样,执行 curl -X POST -d data [URL]...
但是您可以创建一个有趣且有点罕见的请求,该请求在 GET 请求中发送请求正文类似于 curl -X GET -d data [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]
.
curl -GET
(使用单个破折号)对于此目的是错误的.这相当于指定 -G
, -E
和 -T
选项,这将做一些完全不同的事情.
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.
还有一个名为 --get
不要将问题与两者混淆.是-G的长格式,用于转换-d指定的数据
变成 GET 请求而不是 POST.
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.
(我随后在此处使用了我自己的答案来填充 curl 常见问题解答以涵盖此内容.)
(I subsequently used my own answer here to populate the curl FAQ to cover this.)
当启用详细模式 (-v
) 时,curl 的现代版本将通知用户这种不必要且可能有害的 -X 使用 - 让用户意识到.这篇博文一>.
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.
您可以要求 curl 转换一组 -d
选项,而不是使用 POST 在请求正文中发送它们,而是将它们放在 URL 查询字符串的末尾并发出 GET,使用使用`-G.像这样:
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/
这篇关于curl -GET 和 -X GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!