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

Phydeaux's avatar
Phydeaux
Explorer | Level 3
5 years ago

Powershell File Upload - Bad Request

Hello,

 

First off, I did search the forums. This post , titled the same as this help request, seemed similar but was not helpful.

 

 

I have the following simple curl upload working fine:

curl -X POST https://content.dropboxapi.com/2/files/upload \
    --header "Authorization: Bearer MyKeyGoesHereButThisIsNotReallyIt" \
    --header "Dropbox-API-Arg: {\"path\": \"/test.txt\",\"mode\": \"add\",\"autorename\": true,\"mute\": false,\"strict_conflict\": false}" \
    --header "Content-Type: application/octet-stream" \
    --data-binary @test.txt

And this is that same request converted to powershell - as you can see, I'm using the same API args. 

 

$arg = '{ "path": "test.txt", "mode": "add" "autorename": true, "mute": false, "strict_conflict:" false }'
$authorization = "Bearer MyKeyGoesHereButThisIsNotReallyIt"

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", $authorization)
$headers.Add("Dropbox-API-Arg", $arg)
$headers.Add("Content-Type", 'application/octet-stream')
 
$response = Invoke-RestMethod -Uri https://content.dropboxapi.com/2/files/upload -Method Post -InFile test.txt -Headers $headers

 

 

However, I'm getting back Error 400, Bad Request. 

 

Why is this throwing a bad request error? I'm trying with the same args in both, same file to upload, but with Powershell, I just get this error. I'm not making any changes to the app between the two. 

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

    Can you print out the response body for the call made by the PowerShell code? It should contain a more useful error message indicating what the issue is in that case.

    • Heinek's avatar
      Heinek
      Explorer | Level 4

      Hey, I know i'm necroing this thread, but I'm in the exact same situation as OP, and hope you've somehow seen and solved other cases since.

       

      Works fine in insomnia, but powershell refuses to cooperate.

      Ive put it in a try catch, but there is no info in the exception what so ever.

       

      Edit:

      As always, the solution comes after you asked someone for help.

      I had not considered that the target path and filename was not in quotes, so the json was wrong. I use arguments in azure pipelines, so it eluded me completely.

       

      Ended up with this:

      $targetPathAndFilename = ' "/Folder/file.exe" '
      
      $arg = '{ "path": '+$targetpathAndFilename+', "mode": "add", "autorename": true, "mute": false }'

       

       

       

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

        I'm glad to hear you already sorted this out. 

         

        For future reference, or for anyone else in a similar scenario, make sure you read out the response body, as it will generally contain a more specific error message which can help you debug issues like this. It sounds like PowerShell doesn't show that by default, but you can read it as described in this thread.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,915 PostsLatest Activity: 24 hours ago
333 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!