LoginSignup
1
0

【PowerShell】Invoke-WebRequest の使い方

Posted at

はじめに

Invoke-WebRequest を使った API 呼び出しの利用方法を備忘録として記載。

実行環境

  • macOS Sonoma 14.3
  • PowerShell 7.3.1

使い方

Invoke-WebRequest [-Uri] <Uri> [-Method <WebRequestMethod>]

オプションは色々とあるみたいですが、今回も -Uri-Method のみ指定して実行します。
(仕事で利用したときは、認証トークンを渡すために -Headers も利用してました。

実行に使ってみる

今回も PokeAPI から 推しポケモンのエーフィの情報を取得してみます。

実行コマンド

Invoke-WebRequest -Method GET -Uri https://pokeapi.co/api/v2/pokemon/espeon

実行結果

StatusCode        : 200
StatusDescription : OK
Content           : {"abilities":[{"ability":{"name":"synchronize","url":"https
                    ://pokeapi.co/api/v2/ability/28/"},"is_hidden":false,"slot"
                    :1},{"ability":{"name":"magic-bounce","url":"https://pokeap
                    i.co/api/v2/ability/156…
RawContent        : HTTP/1.1 200 OK
                    Date: Sun, 04 Feb 2024 05:14:22 GMT
                    Transfer-Encoding: chunked
                    Connection: keep-alive
                    Access-Control-Allow-Origin: *
                    Cache-Control: public, max-age=86400, s-maxage=86400
                    ETag: W/"373b1…
Headers           : {[Date, System.String[]], [Transfer-Encoding, System.String
                    []], [Connection, System.String[]], [Access-Control-Allow-O
                    rigin, System.String[]]…}
Images            : {}
InputFields       : {}
Links             : {}
RawContentLength  : 226225
RelationLink      : {}

Invoke-RestMethod と Invoke-WebRequest の違い

前回書いた Invoke-RestMethod との違いについてですが、下記記事に記載がありました。

(ありがとうございます。)

結論としては、

Content の中身だけを使いたい場合 = Invoke-RestMethod
StatusCoke など、Content 以外の情報も使いたい場合 = Invoke-WebRequest

さいごに

今回は、PowerShellで Invoke-WebRequest を使った API 呼びだしを記事にしてみました。
誰かの役に立てば幸いです。

1
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
1
0