powerShell如何发起post请求提交json参数?

我这样写,服务端提示字段没有被双引号包裹

curl.exe -k -X POST -H 'Content-Type: application/json' --data '{"sn":"sn01"}' 'https://192.168.80.100:8090/simulate'  

这样也不行

curl.exe -k -X POST -H 'Content-Type: application/json' --data "{\"sn\":\"sn01\"}" 'https://192.168.80.100:8090/simulate' 
阅读 227
2 个回答
$body = @{
    sn = "sn01"
} | ConvertTo-Json

Invoke-RestMethod -Uri 'https://192.168.80.100:8090/simulate' -Method Post -Body $body -ContentType 'application/json' -SkipCertificateCheck
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Header-Key", "Header-Value")

$body = "{`n `"key`": `"value`"`n}"

$response = Invoke-RestMethod 'https://httpbin.org/path/to/api' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进