cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: testing API in Powershell: "expected null, got value"

testing API in Powershell: "expected null, got value"

Jon P.4
New member | Level 2
Go to solution

After looking through posts as well as the .ps1 files being used in the AD sync tool, I was trying to run some simple data gathering api calls to get a feel for things however when I try to run a 'get_current_user' I get an error that it was expecting a null body but received data.  If I forgoe adding a body property, it states 

"Invoke-RestMethod : Error in call to API function "users/get_current_account": request body: could not decode input as JSON".

 

$authorization = "Bearer [redacted]"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", $authorization)
$ContentType = 'application/json;charset=utf-8'
$uri = "https://api.dropboxapi.com/2/users/get_current_account"
$json = "{}"
$body = ([System.Text.Encoding]::UTF8.GetBytes($json))
Invoke-RestMethod -Uri $uri -Headers $headers -Body $body -ContentType $ContentType -Method post

 

Error:

 

Invoke-RestMethod : Error in call to API function "users/get_current_account": request body: expected null, got value
At line:8 char:1
+ Invoke-RestMethod -Uri $uri -Headers $headers -Body $body -ContentTyp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

When making an API call like this that takes no parameters, you can do it one of two ways:

 

  1. Don't supply any request body, and accordingly omit the Content-Type request header.
  2. Supply just "null" as the request body, and submit the Content-Type request header as "application/json".

 

As you have it, you're submitting a non-null JSON body, which is unexpected for this call, as it doesn't expect any parameters.

 

Also, apologies, this is confusing in this case, as you're only supplying an empty dictionary, but as it's built right now, the API won't accept that. The simplest solution in this case is probably just to change your "{}" to "null".

View solution in original post

3 Replies 3

Greg-DB
Dropbox Staff
Go to solution

When making an API call like this that takes no parameters, you can do it one of two ways:

 

  1. Don't supply any request body, and accordingly omit the Content-Type request header.
  2. Supply just "null" as the request body, and submit the Content-Type request header as "application/json".

 

As you have it, you're submitting a non-null JSON body, which is unexpected for this call, as it doesn't expect any parameters.

 

Also, apologies, this is confusing in this case, as you're only supplying an empty dictionary, but as it's built right now, the API won't accept that. The simplest solution in this case is probably just to change your "{}" to "null".

Jon P.4
New member | Level 2
Go to solution

Hey Greg, 

 

Option 1 did not seem to work for me:

 Bad HTTP "Content-Type" header: "application/x-www-form-urlencoded".  Expecting one of "application/json"

 

Option 2 however did work properly.  I thought I had tried that before but apparently was sending

$json = $null 

instead of 

$json = "null"

 

Thanks for the assistance!

 

Edit with full code of what worked:

$authorization = "Bearer [redacted auth key]"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", $authorization)
$ContentType = 'application/json'
$uri = "https://api.dropboxapi.com/2/users/get_current_account"
$json = "null"
$body = ([System.Text.Encoding]::UTF8.GetBytes($json))
Invoke-RestMethod -Uri $uri -Headers $headers -Method post -body $body -ContentType $ContentType

axew3
Collaborator | Level 8
Go to solution

"The simplest solution in this case is probably just to change your "{}" to "null"."

yes it is:
$data = "null";

Need more support?