Upgrade to Pro — share decks privately, control downloads, hide ads and more …

知っておくと便利なcurl

4a05d4c03e5f9c6f78ec4b3c57dc63c4?s=47 oohira
July 23, 2015

 知っておくと便利なcurl

4a05d4c03e5f9c6f78ec4b3c57dc63c4?s=128

oohira

July 23, 2015
Tweet

Transcript

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

  2. cURLとは?

  3. curl as DSL

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

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

  6. $ 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" } リクエストパラメーターを指定
  7. $ 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でフォーム送信
  8. $ 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送信
  9. $ 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でファイル送信
  10. $ 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 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <title>Redirecting...</title> <h1>Redirecting...</h1> <p>You should be redirected automatically to target URL: <a href="/get">/get</a>. If not click the link. レスポンスヘッダを表示
  11. $ 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
  12. 進捗表示なし $ curl –s http://httpbin.org/image/png > image.png

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

    } Basic認証
  14. $ 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" } } カスタムヘッダを指定
  15. 出力を加工 $ curl -w '%{http_code} %{time_total}sec\n' -s -o /dev/null 'http://httpbin.org/not-found‘

    404 0.404sec
  16. $ man curl