Forum Discussion

Phydeaux's avatar
Phydeaux
Explorer | Level 3
6 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. 

3 Replies

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    6 years ago

    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
    5 years ago

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

    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.

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!