Invoke-WebRequest Vs Invoke-RestMethod

blimeyoops·2023년 8월 25일

powershell

목록 보기
8/8

Powershell에는 HTTP/REST 호출을 수행하기 위한 Invoke-WebRequest 및 Invoke-RestMethod cmdlet이 있다.

Invoke-WebRequest

이 cmdlet은 PowerShell 3.0에서 도입되었고 HTTP, HTTPS, FTP 및 FILE 요청을 웹 페이지 또는 웹 서비스로 보내고 응답으로 링크, 이미지 및 기타 HTML 요소 컬렉션을 반환한다.
REST API 호출, 웹 호출 및 리소스를 다루는 모든 일반적인 HTTP 동사를 지원하고 응답에는 헤더, 상태 코드 및 응답 내용이 포함된다.

Invoke-WebRequest -Uri https://blogs.msdn.microsoft.com/powershell/feed/

StatusCode        : 200
StatusDescription : OK
Content           : <?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
                        xmlns:content="http://purl.org/rss/1.0/modules/content/"
                        xmlns:wfw="http://wellformedweb.org/CommentAPI/"
                        xmlns:dc="http://purl.org/dc/eleme...
RawContent        : HTTP/1.1 200 OK
                    Link: <https://devblogs.microsoft.com/powershell/wp-json/>; rel="https://api.w.org/",<https://devblogs.microsoft.com/powershell/>; rel="canonical"
                    X-Cacheable: CacheAlways: feed
                    X-W...
Forms             : {}
Headers           : {[Link, <https://devblogs.microsoft.com/powershell/wp-json/>; rel="https://api.w.org/",<https://devblogs.microsoft.com/powershell/>; rel="canonical"], [X-Cacheable, CacheAlways: feed], [X-WPE-Request-ID, d461337fe9550436e1c484fd71a7958e], [Transfer-Encoding, 
                     chunked]...}
Images            : {@{innerHTML=; innerText=; outerHTML=<IMG>; outerText=; tagName=IMG}, @{innerHTML=; innerText=; outerHTML=<IMG alt="VM stat screenshot" src="https://devblogs.microsoft.com/powershell/wp-content/uploads/sites/30/2023/07/vm_stat.png" decoding="async">; outerTe 
                    xt=; tagName=IMG; alt=VM stat screenshot; src=https://devblogs.microsoft.com/powershell/wp-content/uploads/sites/30/2023/07/vm_stat.png; decoding=async}, @{innerHTML=; innerText=; outerHTML=<IMG alt="screenshot showing predictive intellisense support" src="h 
                    ttps://devblogs.microsoft.com/powershell/wp-content/uploads/sites/30/2023/07/predictiveintellisense.png" decoding="async">; outerText=; tagName=IMG; alt=screenshot showing predictive intellisense support; src=https://devblogs.microsoft.com/powershell/wp-cont 
                    ent/uploads/sites/30/2023/07/predictiveintellisense.png; decoding=async}, @{innerHTML=; innerText=; outerHTML=<IMG class="alignnone size-full wp-image-20014" alt="Image SuggestionFramework png" src="https://devblogs.microsoft.com/powershell/wp-content/upload 
                    s/sites/30/2023/07/SuggestionFramework.png" width=2840 height=576 decoding="async" sizes="(max-width: 2840px) 100vw, 2840px" srcset="https://devblogs.microsoft.com/powershell/wp-content/uploads/sites/30/2023/07/SuggestionFramework.png 2840w, https://devblogs 
                    .microsoft.com/powershell/wp-content/uploads/sites/30/2023/07/SuggestionFramework-300x61.png 300w, https://devblogs.microsoft.com/powershell/wp-content/uploads/sites/30/2023/07/SuggestionFramework-1024x208.png 1024w, https://devblogs.microsoft.com/powershell 
                    /wp-content/uploads/sites/30/2023/07/SuggestionFramework-768x156.png 768w, https://devblogs.microsoft.com/powershell/wp-content/uploads/sites/30/2023/07/SuggestionFramework-1536x312.png 1536w, https://devblogs.microsoft.com/powershell/wp-content/uploads/site 
                    s/30/2023/07/SuggestionFramework-2048x415.png 2048w">; outerText=; tagName=IMG; class=alignnone size-full wp-image-20014; alt=Image SuggestionFramework png; src=https://devblogs.microsoft.com/powershell/wp-content/uploads/sites/30/2023/07/SuggestionFramework 
                    .png; width=2840; height=576; decoding=async; sizes=(max-width: 2840px) 100vw, 2840px; srcset=https://devblogs.microsoft.com/powershell/wp-content/uploads/sites/30/2023/07/SuggestionFramework.png 2840w, https://devblogs.microsoft.com/powershell/wp-content/up 
                    loads/sites/30/2023/07/SuggestionFramework-300x61.png 300w, https://devblogs.microsoft.com/powershell/wp-content/uploads/sites/30/2023/07/SuggestionFramework-1024x208.png 1024w, https://devblogs.microsoft.com/powershell/wp-content/uploads/sites/30/2023/07/Su 
                    ggestionFramework-768x156.png 768w, https://devblogs.microsoft.com/powershell/wp-content/uploads/sites/30/2023/07/SuggestionFramework-1536x312.png 1536w, https://devblogs.microsoft.com/powershell/wp-content/uploads/sites/30/2023/07/SuggestionFramework-2048x4 
                    15.png 2048w}...}
InputFields       : {}
Links             : {@{innerHTML=PSResourceGet Preview 24 is Now Available; innerText=PSResourceGet Preview 24 is Now Available; outerHTML=<A href="https://devblogs.microsoft.com/powershell/psresourceget-preview-24-is-now-available/" rel=nofollow>PSResourceGet Preview 24 is Now 
                     Available</A>; outerText=PSResourceGet Preview 24 is Now Available; tagName=A; href=https://devblogs.microsoft.com/powershell/psresourceget-preview-24-is-now-available/; rel=nofollow}, @{innerHTML=PowerShell Team; innerText=PowerShell Team; outerHTML=<A hre 
                    f="https://devblogs.microsoft.com/powershell" rel=nofollow>PowerShell Team</A>; outerText=PowerShell Team; tagName=A; href=https://devblogs.microsoft.com/powershell; rel=nofollow}, @{innerHTML=the PowerShell Gallery; innerText=the PowerShell Gallery; outerHT 
                    ML=<A href="https://www.powershellgallery.com/packages/Microsoft.PowerShell.PSResourceGet/0.5.24-beta24" rel=nofollow>the PowerShell Gallery</A>; outerText=the PowerShell Gallery; tagName=A; href=https://www.powershellgallery.com/packages/Microsoft.PowerShel 
                    l.PSResourceGet/0.5.24-beta24; rel=nofollow}, @{innerHTML=; innerText=; outerHTML=<A aria-hidden=true id=user-content-how-to-install-the-module class=anchor href="https://github.com/PowerShell/PowerShell-Blog/pull/231/files#how-to-install-the-module"></A>; o 
                    uterText=; tagName=A; aria-hidden=true; id=user-content-how-to-install-the-module; class=anchor; href=https://github.com/PowerShell/PowerShell-Blog/pull/231/files#how-to-install-the-module}...}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 82832

Invoke-RestMethod

이 cmdlet도 PowerShell 3.0에서 도입되었고 HTTP 및 HTTPS 요청을 REST(Representational State Transfer) 웹 서비스로 보낸다. Invoke-RestMethod는 일부 자동 변환을 수행하는데 예를 들어 API가 JSON을 반환하는 경우 Invoke-RestMethod는 JSON 변환 결과인 PowerShell 개체를 반환한다.
위 Invoke-WebRequest 호출을 Invoke-RestMethod를 사용하면 다음과 같이 호출된다.

Invoke-RestMethod -Uri https://blogs.msdn.microsoft.com/powershell/feed/

title       : PSResourceGet Preview 24 is Now Available
link        : https://devblogs.microsoft.com/powershell/psresourceget-preview-24-is-now-available/
comments    : {https://devblogs.microsoft.com/powershell/psresourceget-preview-24-is-now-available/#respond, 0}
creator     : creator
pubDate     : Wed, 16 Aug 2023 18:03:23 +0000
category    : {category, category}
guid        : guid
description : description
encoded     : encoded
commentRss  : https://devblogs.microsoft.com/powershell/psresourceget-preview-24-is-now-available/feed/

title       : Announcing PowerShell Crescendo 1.1.0-RC1
link        : https://devblogs.microsoft.com/powershell/announcing-powershell-crescendo-1-1-0-rc1/
comments    : {https://devblogs.microsoft.com/powershell/announcing-powershell-crescendo-1-1-0-rc1/#respond, 0}
creator     : creator
pubDate     : Tue, 08 Aug 2023 16:05:27 +0000
category    : {category, category}
guid        : guid
description : description
encoded     : encoded
commentRss  : https://devblogs.microsoft.com/powershell/announcing-powershell-crescendo-1-1-0-rc1/feed/
.
.

결론

Invoke-RestMethod의 경우 XML, JSON 등의 결과 처리에 용이하고, Invoke-Webrequest의 경우 단순 파일 다운로드나 HTML 요청 처리에 용이하다.


참고

0개의 댓글