简体   繁体   English

将 cURL 转换为 PowerShell Invoke-WebRequest - JSON 数据表问题

[英]Convert cURL to PowerShell Invoke-WebRequest - JSON data table issue

I have the following PowerShell script that uses cURL in Windows 10 and works perfectly:我有以下 PowerShell 脚本,它在 Windows 10 中使用 cURL 并且运行良好:

$Body = @{
 'data' = @{
 'CID'                       = 15;
 'HID'                       = 37;
 'Type'                      = "TYPE1";
 'downloadOn'                = "NEXT_CONTACT";
 'AutomationEnabled'         = "True";
 }
}

$CurlArgument = '-s', '-X', 'PATCH',
'-H', 'Content-Type: application/json',
$URL,
'-H', 
$AuthBearer,
'-d', 
(($Body | ConvertTo-Json) -replace '"', '\"')

Write-Host "$Section cURL command took" ("{0:n1}" -f (Measure-Command {$UpdateResponse = & $CURLEXE @CurlArgument}).TotalSeconds) "Seconds"  -ErrorAction SilentlyContinue

I can't use cURL, I need to use native Invoke-WebRequest on my production servers.我不能使用 cURL,我需要在我的生产服务器上使用本机 Invoke-WebRequest。 I need to convert the above into a Invoke-WebRequest command, which I have done, as follows:我需要将上面的转换成一个我已经完成的Invoke-WebRequest命令,如下:

$Body = @{
 'data' = @{
 'CID'                       = 15;
 'HID'                       = 37;
 'Type'                      = "TYPE1";
 'downloadOn'                = "NEXT_CONTACT";
 'AutomationEnabled'         = "True";
 }
}
(($Body | ConvertTo-Json) -replace '"', '\"')

$Method = "PATCH" 
$Header = @{"Accept" = "*/*" ; "Cache-Control" = "no-cache" ; "Host" = "myURL"; "accept-encoding" = "gzip,deflate"; "Authorization" = "Bearer $SessionToken" }
$ContentType = "application/json"

Write-Host "$Section Invoke-WebRequest command took" ("{0:n1}" -f (Measure-Command { $UpdateResponse = Invoke-WebRequest -Method $Method -Uri $URL -Header $Header -ContentType $ContentType -Body $Body }).TotalSeconds) "Seconds"

When I run the Invoke-WebRequest, I get the following error, ie A JSONObject text must begin with '{':当我运行 Invoke-WebRequest 时,我收到以下错误,即 JSONObject 文本必须以 '{' 开头:

Invoke-WebRequest : {"status":"FAILURE","errors":...."message":{"5011":"A JSONObject text must begin with '{' at 1 [character 2 line 1]"}}]}

My $Body looks like this ie it begins with '{':我的 $Body 看起来像这样,即它以'{'开头:

{
\"data\":  {
             \"downloadOn\":  \"NEXT_CONTACT\",
             \"HID\":  37,
             \"AutomationEnabled\":  \"True\",
             \"CID\":  15,
             \"Type\":  \"TYPE1\"
         }
}

I tried with and without "-replace '"', '\"'", from this post" cURL to PowerShell - Double hash table in --data?我从这篇文章“ cURL 到 PowerShell - Double hash table in --data?

Looking at my $Body JSON "object"(?), I can see this:看着我的 $Body JSON “对象”(?),我可以看到:

Name                           Value
----                           -----
data                           {downloadOn, HID, AutomationEnabled, CID...}

Looking at my Value, I can see it is listed as follows:查看我的价值,我可以看到它列出如下:

Name                           Value
----                           -----
downloadOn                     NEXT_CONTACT
HID                            37
AutomationEnabled              True
CID                            15
Type                           TYPE1

Instead of sending -Body $Body,I thought maybe I should just sent the values, as follows (which also failed) with the same message.而不是发送-Body $Body,我想也许我应该只发送值,如下(也失败)和相同的消息。

-Body $Body.Values 

I did a heap of searching last night, but I am at a loss on how to convert that into a successful Invoke-WebRequest, and any help would be appreciated.昨晚我做了一堆搜索,但我不知道如何将其转换为成功的 Invoke-WebRequest,任何帮助将不胜感激。

You are sending $Body as a Hashtable, try converting it to JSON您将 $Body 作为哈希表发送,尝试将其转换为 JSON

 $Body = $Body | ConvertTo-Json

If you send $Body before the above line and again after to an echo service you'll see the difference如果您在上述行之前发送 $Body 并在之后再次发送到 echo 服务,您会看到不同之处

Invoke-RestMethod -Method 'POST' -Uri 'https://postman-echo.com/post' -ContentType 'application/json' -Body $Body

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM