curl 基础

2020-06-15  本文已影响0人  芒鞋儿

curl

json placeholder site, could be used to test rest API

另一个restapi测试网站,似乎这个网站更好

也可以用
github的开发者站点

curl website:

curl http://google.com
curl -L http://google.com

-L: incase redirect

其他:

curl -o outputFile url
curl -i https://reqres.in/api/users/2
curl https://reqres.in/api/users/2

-i: 加上header info

curl --head <url>: 仅获取头文件信息
curl -I <url>: 同上 (大写的i)
curl -v verbose: 通讯详细信息
curl --trace <tracelogfile> URL: trace 并把信息输入log file
curl --trace-ascii <tracelogfile> URL: 更详细的信息

curl -H "header detail info title" <weburl>

curl下载图片
找到online 图片的URL

curl -O <pic URL>

可利用jsonplaceholder做一些测试:

curl -X PUT -d "title=hello" https://jsonplaceholder.typicode.com/

DEL可用: -X DELETE

FTP server 上传文件

curl -u <username@xxx.com:passwd> -T <filename> <ftpserverurl>

POST 方法中
curl -d or curl --data 可以把data post 上
例如

\xh$ curl https://reqres.in/api/users -d "user=morhpeus&job=leader"
{"user":"morhpeus","job":"leader","id":"329","createdAt":"2020-06-15T12:47:45.097Z"}%   

再例如使用-X POST 参数:

\xh$ curl -X POST -H "Accept:application/json" https://reqres.in/api/users -d '{"name":"morpheus","job":"leader"}'
{"{\"name\":\"morpheus\",\"job\":\"leader\"}":"","id":"119","createdAt":"2020-06-15T12:50:31.948Z"}%          

-X PUT 例子:

\xh$ curl -X POST -H "Accept:application/json" https://reqres.in/api/users -d '{"name":"morpheus","job":"leader"}'
{"{\"name\":\"morpheus\",\"job\":\"leader\"}":"","id":"119","createdAt":"2020-06-15T12:50:31.948Z"}%                                \xh$ curl -i X PUT https://reqres.in/api/users/2 -d '{"name":"morpheus","job":"leader"}'
curl: (6) Could not resolve host: X
curl: (6) Could not resolve host: PUT
HTTP/2 201 
date: Mon, 15 Jun 2020 12:55:06 GMT
content-type: application/json; charset=utf-8
content-length: 99
set-cookie: __cfduid=de77940a37435f3fefb4675665a159be01592225705; expires=Wed, 15-Jul-20 12:55:05 GMT; path=/; domain=.reqres.in; HttpOnly; SameSite=Lax; Secure
x-powered-by: Express
access-control-allow-origin: *
etag: W/"63-ZwPhtEsla9IDmDiCMe4/65TyQUY"
via: 1.1 vegur
cf-cache-status: DYNAMIC
cf-request-id: 0359a547fc0000f8cb52149200000001
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
server: cloudflare
cf-ray: 5a3c71866a62f8cb-NRT

{"{\"name\":\"morpheus\",\"job\":\"leader\"}":"","id":"156","createdAt":"2020-06-15T12:55:06.356Z"}%      

-X DELETE 例子:

\xh$ curl -X DELETE https://reqres.in/api/users/2

一些特殊用途,比如测试authentication:

curl https://api.github.com/?access_token=OAUTH-TOKEN
curl -H "Authorization:token OAUTH-TOKEN" https://api.github.com
curl 'https://api.github.com/users/whatever?client_id=xxx&client_secret=yyy'
上一篇 下一篇

猜你喜欢

热点阅读