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

Carpetes's avatar
Carpetes
New member | Level 2
2 years ago

Dropbox-API-Arg could not decode input as JSON

#All credit goes to Jackoby, this is me learning powershell by writting and understanding his code myself

function DropBox-Upload {
    [CmdletBinding()]
    param (
        [Parameter (Mandatory = $True, ValueFromPipeline = $True)]
        [Alias("f")]
        [string]$SourceFilePath
    )
    $DropBoxAccessToken = "" #THE TOKEN ONLY LASTS UP TO FOUR HOURS! PLEASE REPLACE WHEN USING
    $outputfile = Split-Path $SourceFilePath -leaf
    $TargetFilePath = "/$outputfile"
    $arg =  '{"path": "' + $TargetFilePath + '"mode": "add", "autorename": true, "mute": false}'
    $auth = "Bearer " + $DropBoxAccessToken
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Authorization", $auth)
    $headers.Add("Dropbox-API-Arg", $arg)
    $headers.Add("Content-Type", 'application/octet-stream')
    Invoke-RestMethod -Uri https://content.dropboxapi.com/2/files/upload -Method Post -InFile $SourceFilePath -Headers $headers
}
 
The last line throws an exception, saying the arg is not valid json?
  • Здравко's avatar
    Здравко
    Legendary | Level 20

    Carpetes, What you're asking for actually? 🧐 You know what's wrong (I can confirm it is definitely - simply visible). 🤦 Just fix it.

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

    Carpetes As Здравко noted, from your code it does look like you are not building valid JSON. For instance, it looks like you're missing a quote and a comma after the $TargetFilePath in your $arg. Make sure you're building valid JSON when making these calls.

     

    For this reason, we generally suggest using some JSON library to build the JSON for you whenever possible (as that would be less error prone), instead of attempting to build the string in your own code.