概要
途中です。
タイトルの通り、Windows環境でRest APIの開発、デバッグに使えるコマンドラインツールのまとめ(になる予定)です。
コマンドラインツールのインストールにchocolateyを使用するものがありますが、chocolateyについては[The package manager for Windows] (https://chocolatey.org/)をご覧ください。
環境
- Windows10 Professional
- PowerShell 5.1
- curl 7.46.0
- HTTPie 0.9.9
- jq 1.5
- gawk 3.1.6
- xmllint 2.9.3
[curl] (https://curl.haxx.se/)
参考
- [curlコマンドによるデータ送信あれこれ] (http://blog.wagavulin.jp/entry/2015/10/18/060938)
インストール
ダウンロードページからアーカイブファイルを取得して適当な場所に展開しbinディレクトリを環境変数pathに通しておきます。
実行時に"コンピューターにVCRUNTIME140.dllがないため..."というエラーが起きる場合は、[Visual Studio 2015 の Visual C++ 再頒布可能パッケージ
] (https://www.microsoft.com/ja-jp/download/details.aspx?id=48145)からRUNTIMEパッケージを取得してインストールします。
バージョンの確認
> curl --version
curl 7.46.0 (x86_64-pc-win32) libcurl/7.46.0 OpenSSL/1.0.2e zlib/1.2.8 WinIDN libssh2/1.6.0
Protocols: dict file ftp ftps gopher http https imap imaps ldap pop3 pop3s rtsp scp sftp smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile SSPI Kerberos SPNEGO NTLM SSL libz
使用例
GET
> curl "http://weather.livedoor.com/forecast/webservice/json/v1?city=400040"
フォームのPOST
> curl -i -X POST "http://example.com/form" -F "user=rubytomato" -F "pass=password"
ファイルアップロード
> curl -i -X POST "http://example.com/upload" -F "uploadfile=@sample.csv;filename=sample.csv;type=text/plain"
cookieの保存
> curl -c cookie.txt -X GET "http://example.com/"
cookieの送信
cookie.txtに保存したcookieの値を送信する。
> curl -b cookie.txt -X GET "http://example.com/"
[HTTPie] (https://httpie.org/)
curlと同じhttpクライアントツールです。
Windows環境でインストールするにはpythonが必要です。この記事ではAnacondaというディストリビューションを利用しました。
参考
- [Download Anaconda Distribution] (https://www.anaconda.com/download/)
インストール
Anacondaをインストールしたら、Anaconda Promptを立ち上げ次のコマンドを実行します。
> pip install --upgrade pip setuptools
> pip install --upgrade httpie
バージョンの確認
> http --version
0.9.9
使用例
GET
> http "http://weather.livedoor.com/forecast/webservice/json/v1?city=400040"
デフォルトでは整形されて表示されます。整形したくない場合はオプションprettyで指定します。
- --pretty=none : 整形しない
- --pretty=colors : 色付きで表示
- --pretty=format : 整形して表示
- --pretty=all : colorsとformatを指定
フォームのPOST
TODO
cookieの保存
cookieを含めたhttp ヘッダーの情報を保存するにはsessionという機能を使います。
http --session=session.json example.com
cookieの送信
http --session=session.json example.com
[jq] (https://stedolan.github.io/jq/)
jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.
JSONデータに対してフィルタリングや整形が行えるコマンドラインツールです。
インストール
コマンドプロンプトを管理者権限で起動して下記のコマンドでインストールします。
> choco install jq
バージョンの確認
> jq --version
jq-1.5
使用例
Livedoorのお天気Webサービス
http://weather.livedoor.com/weather_hacks/webservice
> curl -X GET "http://weather.livedoor.com/forecast/webservice/json/v1?city=400040" | jq .title,.location,.publicTime
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 3987 0 3987 0 0 42414 0 --:--:-- --:--:-- --:--:-- 42414
"福岡県 久留米 の天気"
{
"city": "久留米",
"area": "九州",
"prefecture": "福岡県"
}
"2017-04-19T17:00:00+0900"
[gawk] (http://gnuwin32.sourceforge.net/packages/gawk.htm)
インストール
アーカイブファイルを適当な場所に展開しbinディレクトリを環境変数pathに通しておきます。
バージョンの確認
> gawk --version
GNU Awk 3.1.6
Copyright (C) 1989, 1991-2007 Free Software Foundation.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
使用例
sample.txt
> type sample.txt
yahoo https://www.yahoo.co.jp/ co.jp
google https://www.google.co.jp/ co.jp
amazon https://www.amazon.co.jp/ co.jp
qiita http://qiita.com/ com
行番号の付加
> type sample.txt | gawk "{print FNR, $0}"
1 yahoo https://www.yahoo.co.jp/ co.jp
2 google https://www.google.co.jp/ co.jp
3 amazon https://www.amazon.co.jp/ co.jp
4 qiita http://qiita.com/ com
出力結果のフォーマット
> type sample.txt | gawk "{print $2}"
https://www.yahoo.co.jp/
https://www.google.co.jp/
https://www.amazon.co.jp/
http://qiita.com/
> type sample.txt | gawk "{printf \"%-2d%-10s%-30s%-10s\n\", FNR, $1,$2,$3}"
1 yahoo https://www.yahoo.co.jp/ co.jp
2 google https://www.google.co.jp/ co.jp
3 amazon https://www.amazon.co.jp/ co.jp
4 qiita http://qiita.com/ com
grep
qiitaという文字列を含む行を抜き出す。
> type sample.txt | gawk "/qiita/{printf \"%-2d%-10s%-30s%-10s\n\", FNR, $1,$2,$3}"
4 qiita http://qiita.com/ com
結果を環境変数へセット
> for /f %r in ('type sample.txt ^| gawk "/qiita/{print $2}"') do set RESULT=%r
echo %RESULT%
http://qiita.com/
batファイルに保存して実行する場合
@echo off
for /f %%r in ('type sample.txt ^| gawk "/qiita/{print $2}"') do set RESULT=%%r
xmllint
xmlパーサーです。
- [The XML C parser and toolkit of Gnome] (http://www.xmlsoft.org/index.html)
参考
- [xmllint] (http://xmlsoft.org/xmllint.html)
インストール
[The XML C parser and toolkit of Gnome] (http://www.xmlsoft.org/index.html)にアクセスし、左側のメニューのDownloadsページからプリコンパイルされたWindows版のバイナリのアーカイブファイルをダウンロードします。
アーカイブファイルを適当な場所に展開しbinディレクトリをpath環境変数に通します。
バージョンの確認
> xmllint --version
xmllint: using libxml version 20903
compiled with: Threads Tree Output Push Reader Patterns Writer SAXv1 FTP HTTP DTDValid HTML Legacy C14N Catalog XPath XPointer XInclude Iconv ISO8859X Unicode Regexps Automata Expr Schemas Schematron Modules Debug Zlib
PowerShell
参考
- [Windows で Tail -f をやりたい] (http://neos21.hatenablog.com/entry/2016/10/12/003607)
バージョンの確認
> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.16299.98
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.16299.98
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
tail
- linuxの"tail -f"とは違うようです。
> Get-Content application.log -Wait -Tail 1