Invoke-RestMethod : Cannot send a content-body with this verb-type

Hi everyone,

I’m working with an API and I have the GET working on Postman who generated the code but it’s failing when I run it from PS5 or ISE but working fine from PS7.

I googled this all over and nothing seems to relate or work. I’m thinking this may be some underlying .NET issue but can someone suggest a way around it?

Cheers,

C

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")

$body = @"
{ `"username`": `"[USERNAME]`", `"apikey`": `"[API_KEY]`" }
"@

$response = Invoke-RestMethod 'https://apiv2.[HOST].net/api/v1/dns/zone' -Method 'GET' -Headers $headers -Body $body
$response | ConvertTo-Json

could you share the API you are actually hitting - like what service/API is it or could you share the docs for the API?

Also what do you mean by ‘failing’? Is it erroring out? If so what error message do you get? Very likely a way around it just need more info (and ideally a way we can test ourselves, though that’s not always possible).

FWIW: I also generally avoid the auto generated code things like postman provide. Sometimes a nice to have, but often the code they generate is much more complex than it needs to be.

The error I’m getting is the subject of the thread.

Invoke-RestMethod : Cannot send a content-body with this verb-type.
At line:1 char:13
+ $response = Invoke-RestMethod 'https://apiv2.mtgsy.net/api/v1/dns/zon ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], ProtocolViolationException
    + FullyQualifiedErrorId : System.Net.ProtocolViolationException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

This is the API documentation page: Branding API Specification
You should be able to create a free account but you need a domain with a dns zone to test it which isn’t free.
Yeah, it works absolutely fine in PS7, I really don’t get it!

Also, I only tried Postman after spending some time fighting this. I need to understand the problem and find a workaround to use it in PS5.

I appreciate taking some time to check this. :slight_smile:

Cheers,

C

I didn’t realize the subject line was the acutal error. I suspect you might be right about the .NET based on this: (1) Using Invoke-RestMethod returns ‘cannot send a content-body with this verb-type’ exception when using a GET request. : PowerShell (reddit.com)

It seems like from this post, it’s potentially thinking that a get verb shouldn’t have a content-body, which sounds like what the error is complaining about.

I know the Invoke-Restmethod did have a lot of changes in PS7, so would not be surprised if this was addressed (either directly or indirectly).

This may sound strange but… just omit the entire $headers variable, and see what happens. It very well could fail, but worth a shot.

I still hate that postman is formatting the body and doing the headers like that. Those really just need to be hash tables.

$Body = @{
'Prop1' = 'Value`
}