Hamutaro - Hamtaro 4

Linux

[PowerShell] Invoke-RestMethod, Invoke-WebRequest

carsumin 2024. 4. 30. 09:48

https://www.truesec.com/hub/blog/invoke-webrequest-or-invoke-restmethod

 

Invoke-WebRequest or Invoke-RestMethod? - Trulysuper

Learn when it's appropriate to use either Invoke-WebRequest or Invoke-RestMethod.

www.truesec.com

 

Invoke-RestMethod

RESTful ์›น ์„œ๋น„์Šค์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” cmdlet์ด๋‹ค

์›น API๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  JSON ๋˜๋Š” XML ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค

GET, POST, PUT, DELETE ๋“ฑ์˜ HTTP ๋ฉ”์„œ๋“œ๋ฅผ ์ง€์›ํ•˜๋ฉฐ ์›น ์„œ๋น„์Šค์™€์˜ ํ†ต์‹ ์„ ๊ฐ„ํŽธํ•˜๊ฒŒ ํ•œ๋‹ค

 

๊ธฐ๋ณธ์‚ฌ์šฉ๋ฒ•

Invoke-RestMethod -Uri <URI> [-Method <Method>] [-Body <Body>] [-Headers <Headers>] [-ContentType <ContentType>]

 

  • -Uri : ์š”์ฒญํ•  API์˜ URI
  • -Method : HTTP ๋ฉ”์„œ๋“œ๋กœ ๊ธฐ๋ณธ๊ฐ’์€ GET
  • -Body : ์š”์ฒญ์— ํฌํ•จํ•  ๋ฐ์ดํ„ฐ๋กœ ์ฃผ๋กœ POST๋‚˜ PUT ์š”์ฒญ ์‹œ ์‚ฌ์šฉ๋จ
  • -Headers : ์š”์ฒญ์— ํฌํ•จํ•  HTTP ํ—ค๋”
  • -ContentType : ์š”์ฒญ ๋ณธ๋ฌธ์˜ ์ฝ˜ํ…์ธ  ํƒ€์ž…์œผ๋กœ ๊ธฐ๋ณธ๊ฐ’์€ 'application/json'

1. GET ์š”์ฒญ

Github API ํ˜ธ์ถœํ•˜๋ ค PowerShell Repository ์ •๋ณด ๊ฐ€์ ธ์˜ด

$url = "https://api.github.com/repos/PowerShell/PowerShell"
$response = Invoke-RestMethod -Uri $url
$response | Format-List

 

2. POST ์š”์ฒญ

JSONPlaceholder API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ํฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” POST ์š”์ฒญ์„ ๋ณด๋ƒ„

'ConvertTo-Json' cmdlet ์‚ฌ์šฉํ•˜์—ฌ ํ•ด์‹œํ…Œ์ด๋ธ”์„ JSON ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜

$url = "https://jsonplaceholder.typicode.com/posts"
$body = @{
    title = "foo"
    body = "bar"
    userId = 1
} | ConvertTo-Json

$response = Invoke-RestMethod -Uri $url -Method Post -Body $body -ContentType "application/json"
$response

 

3. PUT ์š”์ฒญ

$url = "https://jsonplaceholder.typicode.com/posts/1"
$body = @{
    id = 1
    title = "foo"
    body = "bar"
    userId = 1
} | ConvertTo-Json

$response = Invoke-RestMethod -Uri $url -Method Put -Body $body -ContentType "application/json"
$response

 

4. DELETE ์š”์ฒญ

$url = "https://jsonplaceholder.typicode.com/posts/1"
Invoke-RestMethod -Uri $url -Method Delete

 

HTTP ํ—ค๋” ์ถ”๊ฐ€

HTTP ํ—ค๋”๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์š”์ฒญ์„ ๋” ์ƒ์„ธํ•˜๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค

์ธ์ฆ ํ† ํฐ์„ ํ—ค๋”์— ํฌํ•จํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ๋‹ค

$url = "https://api.example.com/secure-data"
$headers = @{
    Authorization = "Bearer your_access_token"
}

$response = Invoke-RestMethod -Uri $url -Headers $headers
$response

 

Invoke-WebRequest

์›น ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  ์‘๋‹ต์„ ์ˆ˜์‹ ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” cmdlet์ด๋‹ค

์›น ํŽ˜์ด์ง€์˜ ์ฝ˜ํ…์ธ ๋ฅผ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ฑฐ๋‚˜ REST API์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๋Š” ๋“ฑ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค

HTTP ๋ฐ HTTPS ํ”„๋กœํ† ์ฝœ์„ ์ง€์›ํ•˜๋ฉฐ ๋‹ค์–‘ํ•œ HTTP ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค

 

๊ธฐ๋ณธ ๊ตฌ๋ฌธ

Invoke-WebRequest -Uri <URI> [-Method <Method>] [-Body <Body>] [-Headers <Headers>] [-ContentType <ContentType>] [-OutFile <FilePath>]

 

  • -Uri : ์š”์ฒญํ•  API์˜ URI
  • -Method : HTTP ๋ฉ”์„œ๋“œ๋กœ ๊ธฐ๋ณธ๊ฐ’์€ GET
  • -Body : ์š”์ฒญ์— ํฌํ•จํ•  ๋ฐ์ดํ„ฐ๋กœ ์ฃผ๋กœ POST๋‚˜ PUT ์š”์ฒญ ์‹œ ์‚ฌ์šฉ๋จ
  • -Headers : ์š”์ฒญ์— ํฌํ•จํ•  HTTP ํ—ค๋”
  • -ContentType : ์š”์ฒญ ๋ณธ๋ฌธ์˜ ์ฝ˜ํ…์ธ  ํƒ€์ž…์œผ๋กœ ๊ธฐ๋ณธ๊ฐ’์€ 'application/json'
  • -OutFile : ์‘๋‹ต ๋‚ด์šฉ์„ ํŒŒ์ผ๋กœ ์ €์žฅ

1. GET ์š”์ฒญ

์›น ํŽ˜์ด์ง€์˜ ๋‚ด์šฉ์„ ๊ฐ€์ ธ์™€์„œ ์ถœ๋ ฅํ•จ

์‘๋‹ต์€ 'HtmlWebResponseObject' ๊ฐ์ฒด๋กœ ๋ฐ˜ํ™˜๋˜๋ฉฐ, 'Content' ์†์„ฑ์„ ํ†ตํ•ด HTML ๋‚ด์šฉ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค

$url = "https://www.example.com"
$response = Invoke-WebRequest -Uri $url
$response.Content

 

2. POST ์š”์ฒญ

$url = "https://httpbin.org/post"
$body = @{
    name = "John Doe"
    email = "john.doe@example.com"
}

$response = Invoke-WebRequest -Uri $url -Method Post -Body $body -ContentType "application/x-www-form-urlencoded"
$response.Content

 

3. ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ

์ง€์ •๋œ URL์—์„œ ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜์—ฌ ๋กœ์ปฌ์— ์ €์žฅ

$url = "https://example.com/file.zip"
$output = "C:\path\to\file.zip"
Invoke-WebRequest -Uri $url -OutFile $output

 

4. ํ—ค๋” ์ถ”๊ฐ€

์‚ฌ์šฉ์ž ์ •์˜ ํ—ค๋”๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ API๋ฅผ ํ˜ธ์ถœ

$url = "https://api.example.com/data"
$headers = @{
    "Authorization" = "Bearer your_access_token"
    "Custom-Header" = "CustomValue"
}

$response = Invoke-WebRequest -Uri $url -Headers $headers
$response.Content

 

5. ํผ ๋ฐ์ดํ„ฐ ์ „์†ก

$url = "https://httpbin.org/post"
$form = @{
    field1 = "value1"
    field2 = "value2"
}

$response = Invoke-WebRequest -Uri $url -Method Post -Form $form
$response.Content

 

HTTP ์‘๋‹ต ์ฒ˜๋ฆฌ

 

  • StatusCode : HTTP ์ƒํƒœ ์ฝ”๋“œ (์˜ˆ: 200, 404, 500 ๋“ฑ)
  • StatusDescription : ์ƒํƒœ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์„ค๋ช…
  • Content : ์‘๋‹ต ๋ณธ๋ฌธ
  • Headers : ์‘๋‹ต ํ—ค๋”
  • RawContent : ์›์‹œ ์‘๋‹ต ๋ฐ์ดํ„ฐ
  • Links : HTML ๋ฌธ์„œ์˜ ๋งํฌ ์ปฌ๋ ‰์…˜
  • Forms : HTML ํผ ์ปฌ๋ ‰์…˜
  • ParsedHtml : HTML ๋ฌธ์„œ์˜ ํŒŒ์‹ฑ๋œ ๊ฐ์ฒด
$url = "https://www.example.com"
$response = Invoke-WebRequest -Uri $url

# ์‘๋‹ต ์ƒํƒœ ์ฝ”๋“œ
$response.StatusCode

# ์‘๋‹ต ํ—ค๋”
$response.Headers

# ์‘๋‹ต ๋ณธ๋ฌธ
$response.Content

 

'Linux' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Linux] ์‚ฌ์šฉ์ค‘์ธ ํฌํŠธ ์ข…๋ฃŒํ•˜๊ธฐ (Port Kill)  (0) 2024.11.27