0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

curl コマンドのオプション①

Last updated at Posted at 2023-04-14

curlコマンドのオプション'-X', '-H', '-o' について

REST APIのアクセスに、PostamanなどREST Clientを使用するほか、curlを使用してアクセスできるが、ちっとも覚えられない、そして使えないのでまとめる

きっかけのよくわかんなかったcurlコマンドは以下

curl -X GET -H 'Authorization: Bearer access_token' -o 'myfile.zip' https://example.com/ic/api/endpoint/resource

そもそもcurlコマンドとは
様々な通信プロトコルでデータの送受信を行うことができるコマンド
よく使われるのは、Webサイト等へのHTTPリクエスト
色々なオプションを指定することで、データ取得時の条件、取得情報を変更できる
 

ーX (--request)
HTTP メソッドのを指定(GET,POSTなど)
デフォルト:GET 
PUTなどを使用する際は、特に指定とか必要そう。。。

curl -X GET https://example.com/ic/api/endpoint/resource

-H (--header)
HTTPリクエストのヘッダーなどの情報を渡せる
-H "Content-Type:application/json" とか、、、

curl -H 'Authorization: Bearer ***' -X POST
https://example.com/ic/api/endpoint/resource

-o (--output ) path
ローカル・マシン上のレスポンス出力を指定
エクスポート操作とレスポンスをファイルへ保存するのに使用

curl -X GET -o 'myfile.zip' https://example.com/ic/api/endpoint/resource

この場合、カレントディレクトリに'myfile.zip'でレスポンスが保存される
-o '/Users/USER_NAME/Downloads/myfile.zip'など、pathごと指定してなくて行方不明になったよ。

☆まとめ☆
以下のcurlコマンドは、
HTTP GETメソッドで取得したレスポンスをmyfile.zipとして保存している、らしい!
※そのアクセスの認証はBearer access_token

curl -X GET -H 'Authorization: Bearer access_token' -o 'myfile.zip' https://example.com/ic/api/endpoint/resource

以下、おまけ

curlのバージョンの確認

curl -version
'curl 7.79.1 (x86_64-apple-darwin21.0) libcurl/7.79.1 (SecureTransport) LibreSSL/3.3.6 zlib/1.2.11 nghttp2/1.45.1
Release-Date: 2021-09-22
Protocols: dict file ftp ftps gopher gophers http https imap imaps ldap ldaps mqtt pop3 pop3s rtsp smb smbs smtp smtps telnet tftp 
Features: alt-svc AsynchDNS GSS-API HSTS HTTP2 HTTPS-proxy IPv6 Kerberos Largefile libz MultiSSL NTLM NTLM_WB SPNEGO SSL UnixSockets'

そもそも、curlがMacで使えるか忘れちゃうんだよね。から確認。

とりあえずの -h(--help) してみる

簡易ヘルプが参照できるオプション
にしても、めっちゃ出てきた。。。

curl -h
'Usage: curl [options...] <url>
 -d, --data <data>   HTTP POST data
 -f, --fail          Fail silently (no output at all) on HTTP errors
 -h, --help <category>  Get help for commands
 -i, --include       Include protocol response headers in the output
 -o, --output <file>  Write to file instead of stdout
 -O, --remote-name   Write output to a file named as the remote file
 -s, --silent        Silent mode
 -T, --upload-file <file>  Transfer local FILE to destination
 -u, --user <user:password>  Server user and password
 -A, --user-agent <name>  Send User-Agent <name> to server
 -v, --verbose       Make the operation more talkative
 -V, --version       Show version number and quit'

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?