Slide 1

Slide 1 text

知っておくと便利な curl もくもく発表会 2015/07/23 @oohira

Slide 2

Slide 2 text

cURLとは?

Slide 3

Slide 3 text

curl as DSL

Slide 4

Slide 4 text

$ curl http://httpbin.org/ip { "origin": "113.37.30.210" } URLを渡すとGET

Slide 5

Slide 5 text

$ curl -X GET http://httpbin.org/ip { "origin": "113.37.30.210" } リクエストメソッドを指定

Slide 6

Slide 6 text

$ curl ’http://httpbin.org/get?foo=bar&hoge=fuga' { "args": { "foo": "bar", "hoge": "fuga" }, "headers": { "Accept": "*/*", "Cache-Control": "max-age=259200", "Host": "httpbin.org", "User-Agent": "curl/7.37.1", "Via": "1.1 rt-in-osk:3128 (squid/2.6.STABLE21)" }, "origin": "113.37.30.210", "url": "http://httpbin.org/get?foo=bar&hoge=fuga" } リクエストパラメーターを指定

Slide 7

Slide 7 text

$ curl -X POST --data 'foo=bar&hoge=fuga’ http://httpbin.org/post { "args": {}, "data": "", "files": {}, "form": { "foo": "bar", "hoge": "fuga" }, "headers": { "Accept": "*/*", "Cache-Control": "max-age=259200", "Content-Length": "17", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "curl/7.37.1", "Via": "1.1 rt-in-osk:3128 (squid/2.6.STABLE21)" }, ... } POSTでフォーム送信

Slide 8

Slide 8 text

$ curl -X POST -H 'Content-Type: application/json' --data '{"foo":"bar", "hoge":"fuga”}’ http://httpbin.org/post { "args": {}, "data": "{\"foo\":\"bar\", \"hoge\":\"fuga\"}", "files": {}, "form": {}, "headers": { "Accept": "*/*", "Cache-Control": "max-age=259200", "Content-Length": "28", "Content-Type": "application/json", "Host": "httpbin.org", "User-Agent": "curl/7.37.1", "Via": "1.1 rt-in-osk:3128 (squid/2.6.STABLE21)" }, ... } POSTでJSON送信

Slide 9

Slide 9 text

$ curl -X PUT -F 'file=@/path/to/file' http://httpbin.org/put { "args": {}, "data": "", "files": { "file": "data:application/octet-stream;base64,iVBORw0KGgoAA... }, "form": {}, "headers": { "Accept": "*/*", "Cache-Control": "max-age=259200", "Content-Length": "8289", "Content-Type": "multipart/form-data; boundary=----------...", "Host": "httpbin.org", "User-Agent": "curl/7.37.1", "Via": "1.1 rt-in-osk:3128 (squid/2.6.STABLE21)" }, ... } PUTでファイル送信

Slide 10

Slide 10 text

$ curl –i http://httpbin.org/redirect/1 HTTP/1.0 302 Moved Temporarily Server: nginx Date: Thu, 23 Jul 2015 01:55:25 GMT Content-Type: text/html; charset=utf-8 Content-Length: 215 Location: /get Access-Control-Allow-Origin: * Access-Control-Allow-Credentials: true X-Cache: MISS from rt-in-osk X-Cache-Lookup: MISS from rt-in-osk:3128 Via: 1.0 rt-in-osk:3128 (squid/2.6.STABLE21) Connection: close Redirecting...

Redirecting...

You should be redirected automatically to target URL: /get. If not click the link. レスポンスヘッダを表示

Slide 11

Slide 11 text

$ curl -o image.png http://httpbin.org/image/png % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 8090 100 8090 0 0 38312 0 --:--:-- --:--:-- --:--:-- 38341 ファイルダウンロード $ curl http://httpbin.org/image/png > image.png % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 8090 100 8090 0 0 38312 0 --:--:-- --:--:-- --:--:-- 38341

Slide 12

Slide 12 text

進捗表示なし $ curl –s http://httpbin.org/image/png > image.png

Slide 13

Slide 13 text

$ curl --user 'user:passwd’ http://httpbin.org/basic-auth/user/passwd { "authenticated": true, "user": "user" } Basic認証

Slide 14

Slide 14 text

$ curl -H 'X-MyApp-API-Token: da39a3ee5e6b4b0d3255bf’ http://httpbin.org/headers { "headers": { "Accept": "*/*", "Cache-Control": "max-age=259200", "Host": "httpbin.org", "User-Agent": "curl/7.37.1", "Via": "1.1 rt-in-osk:3128 (squid/2.6.STABLE21)", "X-Myapp-Api-Token": "da39a3ee5e6b4b0d3255bf" } } カスタムヘッダを指定

Slide 15

Slide 15 text

出力を加工 $ curl -w '%{http_code} %{time_total}sec\n' -s -o /dev/null 'http://httpbin.org/not-found‘ 404 0.404sec

Slide 16

Slide 16 text

$ man curl