使用Powershell處理Http Request
(How to handle HTTP request in powershell)
前言
在Linux的世界裡我們可以用cURL發請求去取得Web 上的資源, 而在Windows則可以使用Invoke-WebRequest做到相同的事情
語法
Invoke-WebRequest -Uri $AppUrl -Method Get
例如
$request = 'http://andy.lai.org/ws/2/artist/5b11f4ce-a62d-471e-81fc-a69a8278c7da?inc=aliases&fmt=json'
$response = Invoke-WebRequest $request
得到的response會長這樣
StatusCode : 200 StatusDescription : OK Content : [{"answer":{"action":{"type":"","uri":""},"image":[],"video":[],"title":"","text":"隨便就可以打開了","speak":""},"intent":"itent_on_computer","slots":[]},{"answer":{ "action":{"type":"","uri":""},"image":[],"v... RawContent : HTTP/1.1 200 OK Access-Control-Allow-Origin: * Connection: keep-alive Content-Length: 477 Content-Type: application/json; charset=utf-8 Date: Sat, 06 Jul 2019 02:30:35 GMT ETag: W/"1dd-2FGrI1X5h... Forms : {} Headers : {[Access-Control-Allow-Origin, *], [Connection, keep-alive], [Content-Length, 477], [Content-Type, application/json; charset=utf-8]...} Images : {} InputFields : {} Links : {} ParsedHtml : mshtml.HTMLDocumentClass RawContentLength : 477 |
這是一個HtmlWebResponseObject 的物件
PS C:\Users\andy> $response.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False HtmlWebResponseObject Microsoft.PowerShell.Commands.WebResponseObject
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False HtmlWebResponseObject Microsoft.PowerShell.Commands.WebResponseObject
當我們只想要body的資訊的時, 可以用 ConvertFrom-JSON將response轉換
$request = 'http://andy.org/ws/2/artist/5b11f4ce-a62d-471e-81fc-a69a8278c7da?inc=aliases&fmt=json'
$reponse = Invoke-WebRequest $request |
ConvertFrom-JSON
結果就會變得很單純的Powershell objectPS C:\Users\1410065> $response
answer intent slots
------ ------ ----- @{action=; image=System.Object[]; video=System.Object[]; title=; text=隨便就可以打開了; speak=} itent_on_computer {} @{action=; image=System.Object[]; video=System.Object[]; title=; text=有開wifi嗎!; speak=} itent_on_wifi {} @{action=; image=System.Object[]; video=System.Object[]; title=; text=什麼是序號; speak=} intent_chk_sn {} |
從回傳JSON中篩選結果
也可以用Select 語法篩選出自己感興趣的Property
$request = 'http://andy.org/ws/2/artist/5b11f4ce-a62d-471e-81fc-a69a8278c7da?inc=aliases&fmt=json'
Invoke-WebRequest $request |
ConvertFrom-JSON |
Select title, text
除此之外若篩選的對象是內層物件裡的屬性(Embedding Object), 可以用 -expand 把它提取出來 再做Select
假設Response body 如下
[
{
"answer":{
"action":{
"type":"appOpen",
"uri":""
},
"image":[],
"video":[],
"title":"",
"text":"隨便就可以打開了",
"speak":""
},
"intent":"itent_on_computer",
"slots":[]
}
//...
]
若想要篩出 action.type 的資訊的話, 需要下Select -expand action 先把action資訊提出來, 再用Select type 去篩資訊, 完整指令如下
$request = 'http://andy.org/ws/2/artist/5b11f4ce-a62d-471e-81fc-a69a8278c7da?inc=aliases&fmt=json'
Invoke-WebRequest $request |
ConvertFrom-JSON |
Select -expand action | Select type
下載檔案
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri "https://raw.githubusercontent.com/andy51002000/cloud-formation-example/master/cloud_formation.json" -OutFile ./cloud_formation.json
Ref:
留言
張貼留言