当前位置: 首页 > Linux

非常值得一看curl使用指南

时间:2023-04-06 01:26:38 Linux

介绍curl是一种常用的命令行工具,用于请求web服务器。它的名字意思是客户端URL工具。它的功能非常强大,命令行参数有几十个。如果熟练的话,完全可以替代Postman等图形界面工具。本文介绍其主要命令行参数,作为日常参考,方便参考。内容主要翻译自《curl cookbook》。为了节省篇幅,下面的例子不包括运行时的输出。初学者可以看看我之前写的《curl 初学者教程》。如果没有参数,curl只会发出GET请求。$curlhttps://www.example.com上面的命令向www.example.com发送GET请求,服务器返回的内容会在命令行输出。-A参数指定了客户端的User-Agent头,也就是User-Agent。curl的默认用户代理字符串是curl/[version]。$curl-A'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/76.0.3809.100Safari/537.36'https://google.com上面的命令将User-Agent更改为铬浏览器。$curl-A''https://google.com上面的命令将删除User-Agent标头。也可以直接通过-H参数指定header来改变User-Agent。$curl-H'User-Agent:php/1.0'https://google.com-b参数用于向服务器发送Cookie。$curl-b'foo=bar'https://google.com上面的命令会生成一个headerCookie:foo=bar,并向服务器发送一个名为foo,值为bar的Cookie。$curl-b'foo1=bar'-b'foo2=baz'https://google.com上面的命令发送了两个cookie。$curl-bcookies.txthttps://www.google.com上述命令读取本地文件cookies.txt,其中包含服务器设置的Cookie(见-c参数),并发送给服务器。-c参数将服务器设置的Cookie写入文件。$curl-ccookies.txthttps://www.google.com上面的命令将服务器的HTTP响应设置的Cookie写入文本文件cookies.txt。-d参数用于发送POST请求的数据体。$curl-d'login=emma&password=123'-XPOSThttps://google.com/login#或$curl-d'login=emma'-d'password=123'-XPOSThttps://google.com/login使用-d参数后,HTTP请求会自动添加headerContent-Type:application/x-www-form-urlencoded。并且会自动将请求转为POST方式,所以-XPOST可以省略。-d参数可以读取本地文本文件的数据,发送给服务器。$curl-d'@data.txt'https://google.com/login上面的命令读取data.txt文件的内容,并将其作为数据体发送到服务器。--data-urlencode--data-urlencode参数相当于-d,发送POST请求的数据体,不同的是发送的数据会自动进行URL编码。$curl--data-urlencode'comment=helloworld'https://google.com/login上面代码中,发送的数据helloworld之间有一个空格,需要进行url编码。-e参数用于设置HTTP头Referer,表示请求的来源。curl-e'https://google.com?q=example'https://www.example.com以上命令将Referer标头设置为https://google.com?q=example。-H参数直接加上headerReferer也可以达到同样的效果。curl-H'Referer:https://google.com?q=example'https://www.example.com-F参数用于将二进制文件上传到服务器。$curl-F'file=@photo.png'https://google.com/profile上面的命令会将标头Content-Type:multipart/form-data添加到HTTP请求中,然后使用文件photo.png作为文件字段上传。-F参数可以指定MIME类型。$curl-F'file=@photo.png;type=image/png'https://google.com/profile上面命令指定MIME类型为image/png,否则curl会设置MIME类型为application/octet-溪流。-F参数还可以指定文件名。$curl-F'file=@photo.png;filename=me.png'https://google.com/profile在上面的命令中,原始文件名为photo.png,但是服务器接收到的文件名为我.png-G参数用于构造URL的查询字符串。$curl-G-d'q=kitties'-d'count=20'https://google.com/search上面的命令会发送一个GET请求,实际请求的URL是https://google.com/search?q=k....如果省略--G,将发出POST请求。如果数据需要进行URL编码,可以结合--data--urlencode参数。$curl-G--data-urlencode'comment=helloworld'https://www.example.com-H参数向HTTP请求添加标头。$curl-H'Accept-Language:en-US'https://google.com上面的命令添加了HTTP标头Accept-Language:en-US。$curl-H'Accept-Language:en-US'-H'Secret-Message:xyzzy'https://google.com上面的命令添加了两个HTTP标头。$curl-d'{"login":"emma","pass":"123"}'-H'Content-Type:application/json'https://google.com/login以上命令添加HTTP请求tagheader是Content-Type:application/json,然后用-d参数发送JSON数据。-i参数打印出服务器响应的HTTP标头。$curl-ihttps://www.example.com上面的命令收到服务器响应后,会先输出服务器响应的头部,然后留一个空行,然后输出网页的源代码。-I参数向服务器发送HEAD请求,然后打印服务器返回的HTTP头。$curl-Ihttps://www.example.com以上命令输出服务器对HEAD请求的响应。--head参数相当于-I。$curl--headhttps://www.example.com-k参数指定跳过SSL检查。$curl-khttps://www.example.com以上命令不会检查服务器的SSL证书是否正确。-L参数将使HTTP请求遵循服务器重定向。默认情况下,curl不遵循重定向。$curl-L-d'tweet=hi'https://api.twitter.com/tweet--limit-rate用于限制HTTP请求和响应的带宽,模拟慢速网络环境。$curl--limit-rate200khttps://google.com上面的命令会将带宽限制为每秒200K字节。-o参数将服务器的响应保存为文件,相当于wget命令。$curl-oexample.htmlhttps://www.example.com以上命令会将www.example.com保存为example.html。-O参数将服务器响应保存为文件,使用URL的最后部分作为文件名。$curl-Ohttps://www.example.com/foo/bar.html以上命令将服务器响应保存为名为bar.html的文件。-s参数不会输出错误和进度信息。$curl-shttps://www.example.com一旦上述命令出错,将不会显示错误信息。如果没有错误发生,运行结果将正常显示。如果您希望curl不产生任何输出,您可以使用以下命令。$curl-s-o/dev/nullhttps://google.com-S参数指定只输出错误信息,通常与-o一起使用。$curl-s-o/dev/nullhttps://google.com除非发生错误,否则上述命令不会产生任何输出。-u参数用于设置服务器认证的用户名和密码。$curl-u'bob:12345'https://google.com/login上面的命令设置用户名为bob,密码为12345,然后转换为HTTP头Authorization:BasicYm9iOjEyMzQ1。curl识别URL中的用户名和密码。$curlhttps://bob:12345@google.com/login上面的命令可以识别URL中的用户名和密码,并将其转换为前面例子中的HTTPheader。$curl-u'bob'https://google.com/login上面的命令只设置了用户名,执行后curl会提示用户输入密码。-v参数输出整个通信过程,方便调试。$curl-vhttps://www.example.com--trace参数也可用于调试,将输出原始二进制数据。$curl--trace--https://www.example.com-x参数指定HTTP请求的代理。$curl-xsocks5://james:cats@myproxy.com:8080https://www.example.com上面命令指定HTTP请求通过myproxy.com:8080的socks5代理发送。如果未指定代理协议,则默认为HTTP。$curl-xjames:cats@myproxy.com:8080https://www.example.com在上面的命令中,请求的代理使用HTTP协议。-X参数指定HTTP请求的方法。$curl-XPOSThttps://www.example.com上面的命令向https://www.example.com发出POST请求。_作者:阮一峰原文:http://www.ruanyifeng.com/blo...