You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

Jon P.4's avatar
Jon P.4
New member | Level 2
8 years ago

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

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

  • 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".

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    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's avatar
      Jon P.4
      New member | Level 2

      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's avatar
      axew3
      Collaborator | Level 8

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

      yes it is:
      $data = "null";

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,880 PostsLatest Activity: 2 hours ago
325 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!