PowerShell API Call: Cannot Send a Content-Body with this Verb Type

Kyle Gibson
3 min readFeb 16, 2023

--

Introduction

I came across a problem making API calls with the GET method in PowerShell when required to pass a body in the request. Python handled it fine, but PowerShell did not, giving this error:

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

Here is how I worked around it.

Explanation

This issue arose when working with the Databricks API. The documentation to list directories and notebooks can be found here:

Workspace API 2.0 | Databricks on AWS

You’ll notice that it requires the GET method but asks for data to be passed in the body of the request as well. Specifically, it asks for a key/value pair for the path of the directory that you are looking to retrieve in the API response.

In Python, this request could look something like this:

import requests

# Set Authorization Token (this is a dummy value)
token = 'mysupersecrettoken'

# Set Request URL (this is a dummy value)
url = 'https://adb-1111111111111.1.azuredatabricks.net/api/2.0/workspace/list'

# Set Request Headers
headers = {
'Authorization': f'Bearer {token}',
'Accept': 'application/json'
}

# Set Request Body
body = {
'path': '/Shared/ExampleNotebooks'
}

# Get Request Response
response = requests.get(url, headers = headers, json = body)

# Print response
print(response.json())

When running this script, the Python requests library handles this perfectly fine.

In PowerShell, this request would look something like this:

# Set Authorization Token (this is a dummy value)
$Token = "mysupersecrettoken"

# Set Request URL (this is a dummy value)
$Url = "https://adb-1111111111111.1.azuredatabricks.net/api/2.0/workspace/list"

# Set Request Headers
$Headers = @{
"Authorization" = "Bearer $Token"
"Accept" = "application/json"
}

# Set Request Body
$Body = @{
"path" = "/Shared/ExampleNotebooks"
}

# Get Request Response
$Response = Invoke-WebRequest -Uri $Url -Method Get -Headers $Headers -Body ($Body | ConvertTo-Json)

# Print response
Write-Host($Response | ConvertFrom-Json)

When running this script, the Invoke-WebRequest cmdlet errors out with the following message:

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

Surely PowerShell has a way to handle this, right?

Luckily, I stumbled upon this post…

…that explained I should just try appending the key/value pair as part of the URL instead of in the body of the request.

So now, our PowerShell script looks like this:

# Set Authorization Token (this is a dummy value)
$Token = "mysupersecrettoken"

# Set Request URL (this is a dummy value)
$Url = "https://adb-1111111111111.1.azuredatabricks.net/api/2.0/workspace/list?path=/Shared/ExampleNotebooks"

# Set Request Headers
$Headers = @{
"Authorization" = "Bearer $Token"
"Accept" = "application/json"
}

# Get Request Response
$Response = Invoke-WebRequest -Uri $Url -Method Get -Headers $Headers

Write-Host($Response | ConvertFrom-Json)

And it worked perfectly!

Note that I just added the question mark, and then passed in my key/value pair in the URL instead of in the body of the request.

If you ever run into this issue with PowerShell, trying passing the parameter in the URL instead of the body of the request to see if that works for you.

Thanks for reading!

--

--

Kyle Gibson

Christian, husband, father. Data Engineer at Chick-fil-A.