1

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.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you SO much! Yep. I wasn't actually sending the converted JSON value!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.