powershell

How to convert the code below to a powershell code


how to translate this code, this was taken from The DropBox API Explorer but a don't know how to translate it to a powershell code specifically using Invoke-RestMethod or another way, the thing is that i need list the content of a folder store in dropbox, all this using powershell and security token

POST /2/files/list_folder 
Host: https://api.dropboxapi.com User-Agent: api-explorer-client Authorization: Bearer dropbox_token 
Content-Type: application/json  
{     
"path": "/documentos" 
} 

Solution

  • REST API's are easy to work with in PowerShell. You just need to pass an ordered hash table containing the headers and a string containing the body. If the body is a json string, which appears to be the case, you can create an ordered hash table and pipe it to ConvertTo-Json to produce the string.

    Use the following:

    $BaseAPIPath = "https://replaceWithDropboxBaseApi.com/"
    
    $headers = [ordered]@{
        "Host"          = "https://api.dropboxapi.com"
        "User-Agent"    = "api-explorer-client"
        "Authorization" = "Bearer dropbox_token"
        "Content-Type"  = "application/json"
    }
    
    $body = [ordered]@{
        "path" = "/documentos"
    } | ConvertTo-Json
    
    $result = Invoke-RestMethod -Method Post -Header $headers -Body $body -Uri "$BaseAPIPath/2/files/list_folder"
    

    You will need to replace $BaseAPIPath with the path to the dropbox api (as it was not provided in your question).

    See Invoke-RestMethod