设想
除此之外,Powershell 2.0 没有有用的 cmdlet Invoke-RestMethod。
我无法升级到版本 3,我发现的大多数示例都使用版本 3。
我找到了 this article ,然而,对于我的简单场景来说,这似乎太复杂了。
我需要编写一个以 Json 格式发布数据的 Powershell 脚本,例如
{"Id":5,"Email":"test@com","DataFields":null,"Status":0}
我试过的
我能够获取数据。这是我尝试过的脚本之一。
curl -v --user username:password https://api.dotmailer.com/v2/account-info
但是,当我尝试 POST 时,我无法弄清楚将消息正文放在脚本中的何处。这是我到目前为止所得到的:
curl -v -X POST -H "Accept: application/json" -H "Content-Type: application/json" -u username:password -d '{"Id":5,"Email":"test@com","OptInType":0,"EmailType":0, "DataFields":null,"Status":0}' https://api.dotmailer.com/v2/contacts
返回以下错误:
{"message":"Could not parse the body of the request based on the content type \"application/json\" ERROR_BODY_DOES_NOT_MATCH_CONTENT_TYPE"}*
题
谁能就如何使用 cURL 从 Powershell 发布 Json 数据提出建议?
任何有关为什么我会收到我在 Waht 我尝试过的部分中提到的错误的指示将不胜感激。
谢谢。
最佳答案
请注意 问题是关于 curl.exe
外部程序 ,而不是 PowerShell 的 Invoke-WebRequest
cmdlet(不幸的是,在更高的 PowerShell 版本中被别名为 curl
,除非明确指定了 .exe
扩展名( curl.exe ...
),否则会抢占对外部程序的调用。
不幸且出乎意料的是,你必须\
-escape嵌入"
作为参数传递给外部程序的字符串中的实例 .
因此,即使:
'{"Id":5,"Email":"test@com","DataFields":null,"Status":0}'
应该可以,但由于 long-standing bug ;相反,您必须使用:
'{\"Id\":5,\"Email\":\"test@com\",\"DataFields\":null,\"Status\":0}'
见 this answer想要查询更多的信息。
关于rest - 使用 cURL 的 Powershell 2.0 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24914244/