PowerShell 使用 Invoke-WebRequest 指令發送 HTTP 請求

本文呼叫 JSONPlaceholder 的模擬 API 做示範。

使用 GET 取得 https://jsonplaceholder.typicode.com/todos/1 結果:
    
Invoke-WebRequest -Uri https://jsonplaceholder.typicode.com/todos/1 -Method Get
    

範例回應:
    
Invoke-WebRequest -Uri https://jsonplaceholder.typicode.com/todos/1 -Method Get

StatusCode        : 200
StatusDescription : OK
Content           : {
                      "userId": 1,
                      "id": 1,
                      "title": "delectus aut autem",
                      "completed": false
                    }
RawContent        : HTTP/1.1 200 OK
                    Date: Mon, 10 Feb 2025 15:56:19 GMT
                    Transfer-Encoding: chunked
                    Connection: keep-alive
                    Report-To: {"group":"heroku-nel","max_age":3600,"endpoints":[{"url":"https://nel.heroku.com/re…
Headers           : {[Date, System.String[]], [Transfer-Encoding, System.String[]], [Connection, System.String[]], [Report-To, System.Strin
                    g[]]…}
Images            : {}
InputFields       : {}
Links             : {}
RawContentLength  : 83
RelationLink      : {}
    

可以發現 Headers 的內容並沒有顯示的很完整,如果想要看到全部內容,我們可以將 API 的回應結果放到變數中,然後再從變數中一層一層取得所有資料。

呼叫 API ,並將結果存入 response 變數中:
    
$response = Invoke-WebRequest -Uri https://jsonplaceholder.typicode.com/todos/1 -Method Get
    

查看 response 變數內容。
範例回應:
    
$response

StatusCode        : 200
StatusDescription : OK
Content           : {
                      "userId": 1,
                      "id": 1,
                      "title": "delectus aut autem",
                      "completed": false
                    }
RawContent        : HTTP/1.1 200 OK
                    Date: Mon, 10 Feb 2025 15:57:49 GMT
                    Transfer-Encoding: chunked
                    Connection: keep-alive
                    Report-To: {"group":"heroku-nel","max_age":3600,"endpoints":[{"url":"https://nel.heroku.com/re…
Headers           : {[Date, System.String[]], [Transfer-Encoding, System.String[]], [Connection, System.String[]], [Report-To, System.Strin
                    g[]]…}
Images            : {}
InputFields       : {}
Links             : {}
RawContentLength  : 83
RelationLink      : {}
    

從 response 變數中取得 Headers 資料:
    
$response.Headers

Key                              Value
---                              -----
Date                             {Mon, 10 Feb 2025 15:57:49 GMT}
Transfer-Encoding                {chunked}
Connection                       {keep-alive}
Report-To                        {{"group":"heroku-nel","max_age":3600,"endpoints":[{"url":"https://nel.heroku.com/reports?ts=1737629466&s…
Reporting-Endpoints              {heroku-nel=https://nel.heroku.com/reports?ts=1737629466&sid=e11707d5-02a7-43ef-b45e-2cf4d2036f7d&s=HDY1O…
Nel                              {{"report_to":"heroku-nel","max_age":3600,"success_fraction":0.005,"failure_fraction":0.05,"response_head…
X-Powered-By                     {Express}
X-Ratelimit-Limit                {1000}
X-Ratelimit-Remaining            {998}
X-Ratelimit-Reset                {1737629502}
Vary                             {Origin, Accept-Encoding}
Access-Control-Allow-Credentials {true}
Cache-Control                    {max-age=43200}
Pragma                           {no-cache}
X-Content-Type-Options           {nosniff}
ETag                             {W/"53-hfEnumeNh6YirfjyjaujcOPPT+s"}
Via                              {1.1 vegur}
CF-Cache-Status                  {HIT}
Age                              {18349}
Server                           {cloudflare}
CF-RAY                           {90fd4c118961f9f9-SJC}
Alt-Svc                          {h3=":443"}
Server-Timing                    {cfL4;desc="?proto=TCP&rtt=141725&min_rtt=135926&rtt_var=62570&sent=3&recv=5&lost=0&retrans=0&sent_bytes=…
Content-Type                     {application/json; charset=utf-8}
Expires                          {-1}
    

這裡可以發現 Headers 裡面的 Report-To 內容還不是很完整,我們可以使用下面的方式查看 Report-To 的詳細內容:
    
$response.Headers["Report-To"]
{"group":"heroku-nel","max_age":3600,"endpoints":[{"url":"https://nel.heroku.com/reports?ts=1737629466&sid=e11707d5-02a7-43ef-b45e-2cf4d2036f7d&s=HDY1OozhO2qqIrM7jCPj4QkFvOSy%2Bo%2B%2BC1HAX09eaoM%3D"}]}
    

-Method 參數包含 Get ,還有以下值可以使用:
  • Default
  • Get
  • Post
  • Put
  • Patch
  • Delete
  • Trace
  • Head
  • Merge
  • Options
文章撰寫中...請稍後...

參考資料:
Microsoft.Learn - Invoke-WebRequest

留言