Invoke-RestMethod(PowerShell版のcUrl)でJsonをPOSTする
▼目次
▼このエントリを書いた理由
「React,Angular,Vue.js,React Nativeを使って学ぶ はじめてのフロントエンド開発」の第5章で、自作のAPIサーバに対してPOST, GETリクエストをターミナルから投げる場面がある。
PowerShellだと、標準でcurlを使うと意図した挙動にならなかったため、このエントリを書くに至った。
▼APIへJsonをPOST
PS C:\Users\user> Invoke-RestMethod -Uri "http://foobar.com/endpoint" -Method POST -Body ( @{"cname"="general"} | ConvertTo-Json) -ContentType 'application/json'
▼APIへJsonをPOST
PS C:\Users\user> Invoke-RestMethod -Uri "http://foobar.com/endpoint" -Method GET
▼参考
◯Json形式でのPOSTの方法が書いてある。
rest - How to make a POST request using Powershell if body have a parameter @type - Stack Overflow
◯Microsoftの本家サイト
Invoke-RestMethod
▼覚書
BodyオプションでJson形式のデータを入れる際、ワンライナーで実現するには、ConvertToJson
を使わないといけない