将Windows cURL语法转换为等效的InvokeRestMethod命令

MKANET

我在将Windows中有效的cURL命令转换为等效的PowerShell InvokeRestMethod命令时遇到麻烦。看起来我很接近。我得到回应。但是,API似乎无法理解InvokeRestMethod命令发送的嵌套哈希表元素“域”API似乎可以识别所有其他哈希元素。

cURL命令 (有效)

curl "https://api.rebrandly.com/v1/links" -X POST -H "apikey: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -H "Content-Type: application/json" -d "{ \"destination\": \"http://longurl.com\", \"domain\": {\"fullName\": \"link.domain.com\"}}"

电源外壳:

$body = @{
 "destination"="longurl.com"
 "domain" = @("fullName", "link.domain.com")
} | ConvertTo-Json

$header = @{
 "apikey"="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
 "Content-Type"="application/json"
}

Invoke-RestMethod -Uri "https://api.rebrandly.com/v1/links" -Method 'Post' -Body $body -Headers $header

PS:我也尝试使用以下语法。不幸的是,我得到了相同的结果..其中“域”哈希元素被忽略。

$body = @{
 "destination"="longurl.com"
 "domain[0]" = "fullName"
 "domain[1]" = "link.domain.com"
} | ConvertTo-Json
HAL9256

当使用@(...)方括号时,它将被解释为数组,而不是键值哈希表,后者将转换为[...]JSON数组。只需将大括号括起来的嵌套语句中的括号替换@{...}为哈希表即可:

$body = @{
 "destination"="longurl.com"
 "domain" = @{"fullName" = "link.domain.com"}
} | ConvertTo-Json

产生匹配的JSON:

{
    "destination":  "longurl.com",
    "domain":  {
                   "fullName":  "link.domain.com"
               }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章