Forum Discussion

Carpetes's avatar
Carpetes
New member | Level 2
3 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?

2 Replies

  • Здравко's avatar
    Здравко
    Legendary | Level 20
    2 years ago

    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 Community Moderator rankDropbox Community Moderator
    2 years ago

    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.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

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, Facebook or Instagram.

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!