Simon
Simon

Reputation: 97

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:

$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. I need to convert the above into a Invoke-WebRequest command, which I have done, as follows:

$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, i.e. A JSONObject text must begin with '{':

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

My $Body looks like this i.e. it begins with '{' :

{
\"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?

Looking at my $Body JSON "object"(?), I can see this:

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.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.

Upvotes: 1

Views: 1013

Answers (1)

jfrmilner
jfrmilner

Reputation: 1788

You are sending $Body as a Hashtable, try converting it to 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

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

Upvotes: 1

Related Questions