# Converting a working CURL to PowerShell Invoke-WebRequest

**URL:** <https://forums.powershell.org/t/converting-a-working-curl-to-powershell-invoke-webrequest/12415>\
**Category:** PowerShell Help\
**Created:** [April 18, 2019, 1:00am UTC](https://forums.powershell.org/t/converting-a-working-curl-to-powershell-invoke-webrequest/12415 "2019-04-18T01:00:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![terry](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@terry](https://forums.powershell.org/u/terry)\
**Post date:** [April 18, 2019, 1:00am UTC](https://forums.powershell.org/t/converting-a-working-curl-to-powershell-invoke-webrequest/12415/1 "2019-04-18T01:00:54Z")

</div>

Hello,

I have been writing some PowerShell API utilities converting from working CURL requests. Generally been pretty successful in getting both GET & POST PowerShell equivalents to work. Ran into a brick wall on this POST request below however and just cannot figure out the correct array syntax. I have done simpler arrays OK, but the syntax for the “changes”, component of the CURL --data-binary item is what I am having trouble with (I think). I have tried a lot of combinations based on reading other references out there but still am stumbling on this. Any Help is appreciated…

**The working CURL is:**

[pre]

curl -k ‘[https://10.x.x.x:3000/zx/api/v3/host\_sets/static](https://10.x.x.x:3000/zx/api/v3/host_sets/static)’ -X ‘POST’ --data-binary ‘{“name”:“Test”,“changes”:[{“command”:“change”,“add”:[“b4s42mSuRlbe8F4vwiSJGC”],“remove”:}]}’ -H ‘X-FeApi-Token: IPTzzzaDtYvOYJnaCorqpGnQFUAM123c6u6A3CKzwcASc=’ -H ‘Accept: application/json’ -H ‘Content-Type: application/json’

[/pre]

**My problematic PowerShell conversion:**

[pre]

# 

# API Utility

$FEAPI\_Token=$args[0]  
$Uri = $args[1]  
$postParams = @(@{  
“name”=“example”  
“changes” = @(@{  
“command”=“change”;  
“add”=“b4s42mSuRlbe8F4vwiSJGC”  
})}) | ConvertTo-Json

write-host $postParams

# The write-host above is to just look at what the array looks like.

$Headers = @{  
“Accept”=“application/json”  
“Content-Type”=“application/json”  
‘X-FeApi-Token’ = $FEAPI\_Token  
}

$resp = Invoke-WebRequest -Method Post -Uri $Uri -Headers $Headers -Body $postParams  
write-host $resp

[/pre]

**The output from running the PowerShell:**

```
PS C:\Users\sa-tkinkel> c:\Powershell\FEAPI_PostIt1.ps1 IL7zzxxyyEWUdkBHMffkRxa3kNTid1234IMdIYTAT8= https://10.x.x.x:3000/hx/api/v3/host_sets/static
{
"name": "Test",
"changes": [
{
"add": "b4s42mSuRlbe8F4vwiSJGC",
"command": "change"
}
]
}
Invoke-WebRequest : {"details":[{"type":"error","code":2007,"path":"changes[0].add","message":"should be 
array","details":["array"]}],"route":"/hx/api/v3/host_sets/static","message":"Unprocessable Entity"}
At C:\Powershell\FEAPI_PostIt1.ps1:21 char:9
+ $resp = Invoke-WebRequest -Method Post -Uri $Uri -Headers $Headers -Body $postPa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
```

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [April 18, 2019, 1:16am UTC](https://forums.powershell.org/t/converting-a-working-curl-to-powershell-invoke-webrequest/12415/2 "2019-04-18T01:16:35Z")

</div>

Can you try executing this by hardcoding the json ?

```
$Json = '{"name":"Test","changes":[{"command":"change","add":["b4s42mSuRlbe8F4vwiSJGC"],"remove":[]}]}'
Invoke-RestMethod -Uri $Uri -Body $Json -ContentType Application/Json -Headers @{'X-FeApi-Token' = $FEAPI_Token} -Method Post
```

---

<div class="post-metadata">

**Author:** ![pwshliquori](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/pwshliquori/32/222_2.png) [@pwshliquori](https://forums.powershell.org/u/pwshliquori)\
**Post date:** [April 18, 2019, 5:35am UTC](https://forums.powershell.org/t/converting-a-working-curl-to-powershell-invoke-webrequest/12415/3 "2019-04-18T05:35:17Z")

</div>

Hi Terry,

When using the ConvertTo-Json Cmdlet, if you pipe the PowerShell object to ConvertTo-Json, it compresses the object and will not expand the array you are looking for. Try doing it like this:

```
$FEAPI_Token=$args[0]
$Uri = $args[1]
$postParams = @(
    @{
        "name"="example"
        "changes" = @(
            @{
                "command"="change";
                "add"= @(
                    "b4s42mSuRlbe8F4vwiSJGC"
                )       
            }           
        )   
    }
)

$Body = ConvertTo-Json -InputObject $postParams -Depth 4

$Headers = @{
    "Accept"="application/json"
    "Content-Type"="application/json"
    'X-FeApi-Token' = $FEAPI_Token
}

$resp = Invoke-RestMethod Post -Uri $Uri -Headers $Headers -Body $Body
```

As suggested by @kvprasoon, use the Invoke-RestMethod Cmdlet when calling a Rest API.

pwshliquori

---

<div class="post-metadata">

**Author:** ![terry](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@terry](https://forums.powershell.org/u/terry)\
**Post date:** [April 18, 2019, 9:04am UTC](https://forums.powershell.org/t/converting-a-working-curl-to-powershell-invoke-webrequest/12415/4 "2019-04-18T09:04:32Z")

</div>

[kvprasoon](https://powershell.org/profile/prasoon-karunan-v/)

Hardcoding the JSON worked fine (much simpler). I am going to play with this approach more and see if I can expand on it without getting into trouble. THANK YOU for your response.

[pwshliquori](https://powershell.org/profile/pwshliquori/)

Thanks for responding. The different postParams approach (not sure what the proper nomenclature is; maybe nested array?? still giving me fits) your suggestion did change the response though to:

```
Invoke-RestMethod : {"details":[{"type":"error","code":2007,"path":"","message":"should be 
object","details":["object"]}],"route":"/hx/api/v3/host_sets/static","message":"Unprocessable Entity"}
At C:\Powershell\FEAPI_PostIt3.ps1:43 char:9
+ $resp = Invoke-RestMethod -Method POST -Uri $Uri -Headers $Headers -Body $Body
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
```

**Full Code now with this approach:**
[pre]
# 

# API Utility

$FEAPI\_Token=$args[0]  
$Uri = $args[1]  
$postParams = @(  
@{  
“name”=“example”  
“changes” = @(  
@{  
“command”=“change”;  
“add”= @(  
“b4s42mSuRlbe8F4vwiSJGC”  
)  
}  
)  
}  
)

$Body = ConvertTo-Json -InputObject $postParams -Depth 4write-host $Body

# The write-host above is to just look at what the array looks like.

$Headers = @{  
“Accept”=“application/json”  
“Content-Type”=“application/json”  
“X-FeApi-Token” = $FEAPI\_Token  
}

$resp = Invoke-RestMethod -Method POST -Uri $Uri -Headers $Headers -Body $Body  
write-host $resp

[/pre]

&nbsp;

---

<div class="post-metadata">

**Author:** ![terry](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@terry](https://forums.powershell.org/u/terry)\
**Post date:** [April 22, 2019, 7:59am UTC](https://forums.powershell.org/t/converting-a-working-curl-to-powershell-invoke-webrequest/12415/5 "2019-04-22T07:59:32Z")

</div>

kvprasoon

Hardcoding the JSON worked fine (much simpler). I am going to play with this approach more and see if I can expand on it without getting into trouble. THANK YOU for your response.

---

<div class="post-metadata">

**Author:** ![terry](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@terry](https://forums.powershell.org/u/terry)\
**Post date:** [April 22, 2019, 8:02am UTC](https://forums.powershell.org/t/converting-a-working-curl-to-powershell-invoke-webrequest/12415/6 "2019-04-22T08:02:37Z")

</div>

pwshliquori

Thanks for responding. The different postParams approach (not sure what the proper nomenclature is; maybe nested array?? still giving me fits) your suggestion did change the response though to:Invoke-RestMethod :

```
{"details":[{"type":"error","code":2007,"path":"","message":"should be 
object","details":["object"]}],"route":"/hx/api/v3/host_sets/static","message":"Unprocessable Entity"}
At C:\Powershell\FEAPI_PostIt3.ps1:43 char:9
+ $resp = Invoke-RestMethod -Method POST -Uri $Uri -Headers $Headers -Body $Body
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommandFull Code now with this approach:[pre]## API Utility$FEAPI_Token=$args[0]$Uri = $args[1]$postParams = @(@{"name"="example""changes" = @(@{"command"="change";"add"= @("b4s42mSuRlbe8F4vwiSJGC")})})$Body = ConvertTo-Json -InputObject $postParams -Depth 4write-host $Body# The write-host above is to just look at what the array looks like.$Headers = @{"Accept"="application/json""Content-Type"="application/json""X-FeApi-Token" = $FEAPI_Token}$resp = Invoke-RestMethod -Method POST -Uri $Uri -Headers $Headers -Body $Bodywrite-host $resp[/pre]
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:32pm UTC](https://forums.powershell.org/t/converting-a-working-curl-to-powershell-invoke-webrequest/12415/7 "2024-05-16T20:32:56Z")

</div>


