6
1

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 3 years have passed since last update.

[Curl] Windows でCLIでWeb APIを叩く

Last updated at Posted at 2020-06-21

この記事の目的

curlの備忘録。随時更新予定。

良く使うオプション

# HTTPリクエストメソッドの指定(-X)
> curl -X "GET" "targetURL"

# HTTPヘッダに引数の情報をセットする。(-H, –head)
# windowsの場合は、シングルクォーテーションでパラメータを渡すとエラーになる場合がある。
> curl -H "Authorization: Bearer ${apikey}"

# クエリパラメートをセットする(-d)
> curl -X "POST" -d "{キーバリューデータ}" "targetURL"

# データをセットする。(-F)上と同じだけどファイルをアップロードしたい場合はこちら。
> curl -X "POST" -F "{キーバリューデータ}" "targetURL"

windows環境でのCurl

windowsでcurlする場合、Set-ExecutionPolicy(Powershell)の事前設定と文字化けについて対処する必要がある。

  • cmd(コマンドプロンプト)
    • 文字化けするからchcp 65001コマンドを打つ
  • powershell
    • Set-ExecutionPolicy bypassにしておく。管理者権限にて。
  • windows Terminal
    • powershellと同じ。
  • git Bash
    • 問題なくそのまま使える。

Powershell(windows terminal)でcurlを使うときの注意

powershellでcurlと入力して呼び出されるのはcurlではない。
実際に呼び出されるのは、curlというキーワードにエイリアスされたInvoke-WebRequestというコマンドである。
似て非なるコマンドなので、そのまま使うと渡したオプションによっては、Invoke-WebRequestで対応していないためエラーになる。

下記のコマンドを使ってエイリアスを削除することで"curl"でcurlを呼び出す。などの対応が必要。

> del alias:curl

参考サイト

使用例

LINEにメッセージを送る。

> apikey="あなたのtoken"
> message="hello!!!"
>
> curl -X "POST" -H "Authorization: Bearer ${apikey}" -F "message=${message}" https://notify-api.line.me/api/notify
6
1
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
6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?