U r*_*u s 6 rest powershell curl
除此之外,Powershell 2.0 没有有用的 cmdlet Invoke-RestMethod。
我无法升级到版本 3,我发现的大多数示例都使用版本 3。
我发现这篇文章,但是对于我的简单场景来说似乎太复杂了。
我需要编写一个以 Json 格式发布数据的 Powershell 脚本,例如
{"Id":5,"Email":"test@com","DataFields":null,"Status":0}
Run Code Online (Sandbox Code Playgroud)
我能够获取数据。这是我尝试过的脚本之一。
curl -v --user username:password https://api.dotmailer.com/v2/account-info
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试 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 我尝试过的部分中提到的错误的指示,将不胜感激。
谢谢。
mkl*_*nt0 16
Note that the question is about the curl.exe external program, not about PowerShell's Invoke-WebRequest cmdlet (which, unfortunately, is aliased to curl in later PowerShell versions, preempting calls to the external program unless the .exe extension is explicitly specified (curl.exe ...).
Unfortunately and unexpectedly, you have to \-escape embedded " instances in a string you pass as an argument to an external program in Windows PowerShell and PowerShell (Core) up to v7.2.x.
That is, the following works only in v7.3+:
# Only works in PowerShell 7.3+
'{"Id":5,"Email":"test@com","DataFields":null,"Status":0}'
Run Code Online (Sandbox Code Playgroud)
Windows PowerShell and PowerShell (Core) 7.2.x- require:
# Manual \-escaping of " required in
# *Windows PowerShell* and *PowerShell (Core) 7.2.x-*
'{\"Id\":5,\"Email\":\"test@com\",\"DataFields\":null,\"Status\":0}'
Run Code Online (Sandbox Code Playgroud)
See this answer for more information.
| 归档时间: |
|
| 查看次数: |
36161 次 |
| 最近记录: |