powershell - post api

powershellからformを送信。

# post
$uri = "pie.dev/post"
#$headers = ""
$body = "hello=World"

Invoke-RestMethod -Method Post -Uri $uri -Headers $headers -Body $body -ContentType 'application/x-www-form-urlencoded' | ConvertTo-Json

# PS C:\...\api>.\ApiPost.ps1
# {
#     "args":  {

#              },
#     "data":  "",
#     "files":  {

#               },
#     "form":  {
#                  "hello":  "World"
#              },
#     "headers":  {
#                     "Accept-Encoding":  "gzip",
#                     "Cdn-Loop":  "cloudflare",
#                     "Cf-Connecting-Ip":  "60.126.96.149",
#                     "Cf-Ipcountry":  "JP",
#                     "Cf-Ray":  "848de87c5b71262d-FRA",
#                     "Cf-Visitor":  "{\"scheme\":\"http\"}",
#                     "Connection":  "Keep-Alive",
#                     "Content-Length":  "11",
#                     "Content-Type":  "application/x-www-form-urlencoded",
#                     "Host":  "pie.dev",
#                     "User-Agent":  "Mozilla/5.0 (Windows NT; Windows NT 10.0; ja-JP) WindowsPowerShell/5.1.22621.2506"
#                 },
#     "json":  null,
#     "origin":  "60.126.96.149",
#     "url":  "http://pie.dev/post"
# }

powershellからpost api を呼び出す。

# jsonデータ作成
$data = @{
    name="sato"
    old="20"
    city="tokyo"
};

$jsonData = $data | ConvertTo-Json

# postデータ送信
$uri = "https://pie.dev/post"
#$headers = ""
$body = $jsonData
Invoke-RestMethod -Method Post -Uri $uri -Headers $headers -ContentType "application/json" -Body $body | ConvertTo-Json

# PS C:\...\api>.\ApiPost_2.ps1
# {
#   "args": {},
#   "data": "{\r\n    \"old\":  \"20\",\r\n    \"name\":  \"sato\",\r\n    \"city\":  \"tokyo\"\r\n}",
#   "files": {},
#   "form": {},
#   "headers": {
#     "Accept-Encoding": "gzip",
#     "Cdn-Loop": "cloudflare",
#     "Cf-Connecting-Ip": "60.126.96.149",
#     "Cf-Ipcountry": "JP",
#     "Cf-Ray": "848e0cc4ac3e7379-FRA",
#     "Cf-Visitor": "{\"scheme\":\"https\"}",
#     "Connection": "Keep-Alive",
#     "Content-Length": "67",
#     "Content-Type": "application/json",
#     "Host": "pie.dev",
#     "User-Agent": "Mozilla/5.0 (Windows NT; Windows NT 10.0; ja-JP) WindowsPowerShell/5.1.22621.2506"
#   },
#   "json": {
#     "city": "tokyo",
#     "name": "sato",
#     "old": "20"
#   },
#   "origin": "60.126.96.149",
#   "url": "https://pie.dev/post"
# }

日本語を含む場合の注意。 qiita.com