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