スポンサーリンク
linux環境でAPIを使いたい時、curlコマンドを使うのが一般的なのではないでしょうか。
今回はそんなcurlでよく使うオプションをまとめました。
CLI上で実行できるHTTPクライアントです。
もう少し簡単に言うと(厳密には違いますが)、ブラウザ(FirefoxやChrome)のコマンドライン版とでも考えてもらえればと思います。
要するにWEBサーバにHTTP(S)プロトコルでアクセスし、情報を取得するためのアプリケーションです。
この記事は
での動作を元に記載しています。他のlinuxでは違うかもしれないのでご注意ください。
httpsで接続する時のオプションです。オレオレ証明書(自己証明書)など怪しい証明書でも気にしないよ、というオプションです。
curl: (60) Peer's Certificate issuer is not recognized.
More details here: https://curl.haxx.se/docs/sslcerts.html
curl performs SSL certificate verification by default, using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate, you can specify an alternate file
using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
the -k (or --insecure) option.
上記のようなエラーが出た際は、-k
オプションを付けることで回避できる場合があります。
(本番環境で使うと本当に怪しいサイトにアクセスする場合があると思うので、ご注意を)
HTTPメソッドを指定するオプションです。何も指定しなければGETになります。
例
curl -X POST -d '{"name": "mura", "age": "30"}' http://api.test.net/users
上の例でも少し出ましたが、-d
オプションによりPOSTする時にパラメータを送ることができます。
ちなみに -d で送るとペイロードとして送られるので、(URLに含めるクエリパラメータと違って)他者に見られることはありません。
例
curl -X POST -d '{"name": "mura", "age": "30"}' http://api.test.net/users
上記により、'{"name": "mura", "age": "30"}'
というjsonをWEBサーバに送ることができます。
HTTPの応答で302 Found
のような応答、つまりリクエストされたリソースは別のURLにありますよ
という応答があった場合に、自動でそのURLにアクセスするという動作です。
その場合、Location:
というヘッダに指示されたURLにアクセスします。
例: http://yahoo.co.jpにアクセスした場合
$ curl -D - http://yahoo.co.jp
HTTP/1.1 301 Moved Permanently
~~中略~~
Location: https://www.yahoo.co.jp/
~~後略~~
上記の場合、-L
をつけると自動でhttps://www.yahoo.co.jp
にアクセスします
$ curl -L -D - http://yahoo.co.jp
HTTP/1.1 301 Moved Permanently
Date: Wed, 05 Sep 2018 23:56:05 GMT
P3P: policyref="http://privacy.yahoo.co.jp/w3c/p3p_jp.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Location: https://www.yahoo.co.jp/
Cache-Control: private
Content-Length: 3189
Content-Type: text/html
Age: 0
Connection: keep-alive
### ↑↑ここまで http://yahoo.co.jp のレスポンス ↑↑
### ↓↓ここからhttps://yahoo.co.jpからのレスポンス(リダイレクト先)↓↓
HTTP/2 200
date: Wed, 05 Sep 2018 23:56:05 GMT
p3p: policyref="http://privacy.yahoo.co.jp/w3c/p3p_jp.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
x-frame-options: SAMEORIGIN
vary: Accept-Encoding
expires: -1
pragma: no-cache
cache-control: private, no-cache, no-store, must-revalidate
x-xrds-location: https://open.login.yahooapis.jp/openid20/www.yahoo.co.jp/xrds
content-type: text/html; charset=UTF-8
age: 0
via: http/1.1 edge2114.img.djm.yahoo.co.jp (ApacheTrafficServer [c sSf ])
server: ATS
set-cookie: TLS=v=1.2&r=1; path=/; domain=.yahoo.co.jp; Secure
通常、curlではレスポンスのbodyだけ表示されますが、デバッグの時などは詳細なヘッダが見たいものです。
そういう場合は-D
オプションを付けます。
例
$ curl -D - http://localhost:8880
HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Content-Type: application/json; charset=utf-8
ETag: W/"f36519f1851e9aaeb287b605214616a1"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 169a47f0-dd0a-4e03-946a-7abb3ec16793
X-Runtime: 0.006831
Transfer-Encoding: chunke
このように、詳細なヘッダが表示されます。
なお、上記の例では-D -
となっていますが、2個目のハイフンは標準出力に表示する
というオプションです。
もしヘッダ情報をファイルに出力したければ、ここにファイルを指定します。
###6. -H: ヘッダを指定する
最近ではcurlを使ってAPIを実行する場合が多いかと思いますが、ヘッダにトークンなどを設定する場合があります。
そんな時は`-H`オプションを付ければOKです。
例
$ curl -H "Authorization: Bearer 123456789abcdef" http://localhost:8880
###7. -u: ベーシック認証を行う
WEBページによくあるのがID/PWで認証するベーシック認証です。
curlではこの認証にも対応しています。
`-u "ユーザ名":"パスワード"`と指定すればOKです。
例
$ curl -u testuser:mypassword http://localhost:8880
## まとめ
以上、私がよく使うcurlのオプションです。
昔はwgetばっかり使っていましたが、APIを使うようになってcurlしか使わなくなりましたね~
スポンサーリンク
2022/05/09 02:47:39
hydroxychloroquine cures https://keys-chloroquinehydro.com/
2022/05/09 06:52:33
getting prescription drugs from canada Cefadroxil
2022/05/09 09:28:58
viagra uk 60 mg sildenafil https://aviagrasbt.com/
2022/05/09 21:40:48
bayer levitra coupons medicine levitra https://glevitrargu.com/
2022/05/09 23:15:59
sildenafil for sale online sildenafil 100mg walmart https://sildenafilkiol.com/
2022/05/10 08:10:57
how is levitra different from viagra levitra 40 mg https://xclevitradb.com/
2022/05/10 20:31:38
cialis no prescription overnight how to take cialis 5mg https://dccialisxvf.com/
2022/05/14 09:14:12
canadian pharmacy retin a Prasugrel
2022/05/14 09:19:14
best dissertation writing help
dissertation proposal
dissertation fellowships
2022/05/15 00:21:45
levitra dosing instructions levitra effects https://glevitrargu.com/
2022/05/15 01:57:18
maxifort sildenafil sildenafil dosage for dogs https://sildenafilkiol.com/
2022/05/15 04:19:06
apa dissertation citation
dissertation writing services cost
writing a dissertation proposal
2022/05/15 11:10:35
search levitra viagra cialis and levitra https://xclevitradb.com/
2022/05/16 03:18:59
biology dissertation help
dissertation writing for dummies
phd dissertation editing help
2022/05/17 20:03:14
Jenny
2022/05/18 00:03:08
order provigil 200mg without prescription order modafinil
2022/05/18 04:53:10
what is erythromycin ophthalmic ointment used for
2022/05/18 12:24:58
Geodon buying prescription drugs from canada
2022/05/18 14:56:00
online viagra safe drugstore viagra https://aviagrasbt.com/
2022/05/19 00:08:17
modafinil online cheap modafinil order provigil 200mg without prescription
2022/05/19 01:10:18
buy provigil 100mg for sale order provigil 100mg sale buy modafinil without prescription
2022/05/19 03:29:31
levitra discount card walmart pharmacy prices levitra https://glevitrargu.com/
2022/05/19 03:56:25
buy provigil 200mg generic provigil 200mg provigil 100mg over the counter
2022/05/19 05:07:53
taking sildenafil sildenafil cost cvs https://sildenafilkiol.com/
2022/05/19 14:28:19
buy cheap levitra viagra cialis or levitra https://xclevitradb.com/
2022/05/19 15:58:25
free dissertation writing services
dissertation proposal
cheap dissertation help in houston
2022/05/19 19:51:32
2022/05/19 22:28:24
provigil 200mg price buy modafinil 200mg pills
2022/05/20 02:27:46
cialis without prescriptions uk how much is cialis without insurance? https://dccialisxvf.com/
2022/05/21 01:52:21
provigil 200mg sale buy modafinil 200mg online cheap
2022/05/21 04:55:07
2022/04/07 19:36:08
sildenafil comprar sildenafil 20 mg dosage for ed
2022/04/07 22:27:15
tadalafil generico farmacias del ahorro tadalafil soft tablets
2022/04/08 12:26:18
generic viagra review viagra jelly viagra overnight where to buy viagra
2022/04/08 14:12:02
viagra 50mg free viagra sample pack
2022/04/08 21:52:03
cialis tadalafil cialis brand
2022/04/09 11:46:40
tadalafil or sildenafil is tadalafil illegal
2022/04/11 16:24:46
best drug store mascara buy viagra online canadian pharmacy
2022/04/12 08:59:49
tadalafil plus 21mg buy tadalafil no prescription
2022/04/12 10:51:53
sildenafil citrate 100mg canada sildenafil does it work
2022/04/12 18:46:04
all drugs issued under a prescription_______. accutane canada pharmacy
2022/04/13 01:49:33
sildenafil uk pharmacy where to buy viagra over the counter
2022/04/13 07:51:45
drugstore cowboy southern pharmacy
2022/04/13 10:42:20
target pharmacy refill online motion sickness
2022/04/14 00:31:12
which is best viagra cialis or levitra levitra interactions
2022/04/14 02:20:11
sildenafil for bph viagra non prescription
2022/04/14 10:22:57
is generic cialis available cialis generic 20 mg 30 pills
2022/04/15 01:21:34
is canadian pharmacy legitimate canadian pharmacies requiring prescriptions
2022/04/16 08:55:37
ivermectin sheep drench dosage is ivermectin safe for dogs
2022/04/16 12:08:06
viagra canada drugstore sildenafil 25 mg price
2022/04/17 02:06:28
secure medical online pharmacy india online pharmacy
2022/04/17 03:15:47
tadalafil effects rx tadalafil tablets
2022/04/17 13:22:47
stromectol for scabies merck jeffers ivermectin
2022/04/18 02:58:16
cialis without perscriptions cialis super active reviews
2022/04/19 17:51:52
cialis replacement cialis black brand
2022/04/19 20:32:25
safe canadian pharmacy sites online pharmacy indonesia
2022/04/20 09:30:28
buy generic levitra online levitra generic date
2022/04/20 11:14:19
viagra cialis meds cialis genetic
2022/04/20 21:07:46
cialis vs viagra vs levitra which is better levitra sold in canada
2022/04/21 09:26:13
sildenafil tabs 20mg generic 100mg sildenafil
2022/04/22 22:10:54
canadian pharmacy cialis safe canadian pharmacies to order from
2022/04/23 14:03:34
long term effects of prescription drugs highest rated canadian online pharmacy
2022/04/23 15:43:35
canadian pharmacy xiidra canadian codeine pharmacy
2022/04/24 01:25:18
victoza canadian pharmacy my canadian pharmacy review
2022/04/24 13:53:30
online pharmacy usa legal pharmacy technician
2022/04/29 10:03:17
sildenafil pharmacy uk generic viagra cheapest https://sgiviagraikn.com/
2022/04/29 12:44:00
ivermectin injection for rabbits how to take ivermectin horse paste for scabies https://stromectolujlo.com/
2022/04/30 01:35:25
levitra (vardenafil) brand levitra online pharmacy https://glevitrargu.com/
2022/04/30 03:15:22
sildenafil patent expiration sildenafil 20 mg https://sildenafilkiol.com/
2022/04/30 12:42:31
Top Avana Celexa https://jzpharmacyukj.com/
2022/05/01 00:53:49
online drug store pharmacy prescription https://dtupharmyjn.com/
2022/05/03 12:44:37
global rx pharmacy irvine canadian pharcharmy online no precipitation
2022/05/03 15:17:51
where can you buy viagra in canada buy viagra online india https://aviagrasbt.com/
2022/05/04 14:29:17
levitra interaction levitra 20mg uk https://xclevitradb.com/
2022/05/05 03:06:05
cialis 30 mg do i need a prescription for cialis https://dccialisxvf.com/