Windows 標準のcurl.exeを使ってみる
普段は Httpie を使っているけど、別のWindows PCでメッセージを確認したい場面があったので、Windows 標準のcurl.exeの使い方をメモしておきます。
起動と確認
Power Shell を起動して、「curl.exe」と入力。以下のような反応があれば使えます。
> curl.exe
curl: try 'curl --help' or 'curl --manual' for more information
※「Windows PowerShell」は「curl」が、「Invoke-WebRequest」のエイリアスとして設定されているので、「curl.exe」と入力する必要があります。
「コマンドプロンプト」「PowerShell(Windowsが付かないバージョン)」の場合は、「curl」だけでも動作します。
リクエストとレスポンスの確認
「–verbose」「-v」オプションを指定。
「>」がリクエスト、「<」がレスポンス。
curl.exe --verbose http://localhost/index.html
* Trying ::1:80...
* Connected to localhost (::1) port 80 (#0)
> GET /index.html HTTP/1.1
> Host: localhost
> User-Agent: curl/7.69.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Wed, 27 Apr 2022 03:46:05 GMT
< Server: Apache/2.4.53 (Win64) OpenSSL/1.1.1n PHP/8.1.4
< Last-Modified: Wed, 27 Apr 2022 03:18:46 GMT
< ETag: "1e5-5dd9a4477cfc9"
< Accept-Ranges: bytes
< Content-Length: 485
< Content-Type: text/html
<
<!DOCTYPE html>
<html lang="ja">
<head>
…
POSTでデータを送信
「–data name=value」または 「–d name=value」
curl.exe --data my_name=kero http://localhost/submit.php
<!DOCTYPE html>
<html lang="ja">
<head>
POSTでJSONを送信
curl -X POST -H 'Content-Type:application/json' -d '{"name": "alice"}' localhost:3000/users
ヘッダのみ取得
「–head」をオプション
curl.exe --head http://localhost/index.html
HTTP/1.1 200 OK
Date: Wed, 27 Apr 2022 04:05:08 GMT
Server: Apache/2.4.53 (Win64) OpenSSL/1.1.1n PHP/8.1.4
Last-Modified: Wed, 27 Apr 2022 03:18:46 GMT
ETag: "1e5-5dd9a4477cfc9"
Accept-Ranges: bytes
Content-Length: 485
ディスカッション
コメント一覧
まだ、コメントがありません