How to convert the code below to a powershell code - powershell

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"
}

Note to Asker: This question could be considered low quality as there appears to be little to no research effort. I'm going to leave an answer because it's a fairly simple question. However, questions should avoid asking "Convert this to that language," or "What's the code for a program that does such and such" (or really any other question) without showing a research effort or an attempt to answer your own question. See How to Ask a Good Question
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

Related

How Do I GET a StreamResponseBody and save it as a ZIP file using Powershell

I am trying to extract a streaming response from a standard http GET call using PowerShell.
I tried to do something like using this Invoke-RestMethod method, such as:
$response = Invoke-RestMethod -Uri $uri -Method GET -Headers $headers -Body
and then tried doing various things like accessing the streaming response in this way:
$Stream.Write($response.Content)
or
$Stream.Write($response.RawContentStream)
but nothing works.
In the end, I found out the following command does both the call and the response along with allowing you to save the result to a file directly. This surprised me because the response is a multipart record that includes headers and other data, not just the chunked file in the stream. But the folks at MS apparently knew that 99% of the time with a chunked reply, the user only wants to save the streamed chunks of data into the file, without the other response header data.
$Response = Invoke-WebRequest #Params -Headers $headers -outfile $TempZipFileName
The key here is using the Invoke-WebRequest call with the -outfile option. This saved me a lot of headache and was super simple to use.

Complex queries with Invoke-RestMethod in Powershell

I am trying to invoke an API in Powershell with GET requests. I am familiar with filtering the response by sending a Powershell hashtable in the "Body" parameter as"
Invoke-RestMethod -Uri $uri -Body $filters -Headers $headers
But the hashtables only allow me to filter with the equals operator. As the hashtable looks sort of like
$filter = #{id="fl201"; name="john"}
I need to use comparisons other than "equals", more importantly "-ne" and "-like" and so on. I can filter them after I get the response from Invoke-RestMethod but the response will be so huge that the request itself times out. The API is only a middleware between the client and the database and does nothing other than looking for rows that match the values of the fields in the body.
So, is there any way I can do complex filters along with the web request? Thanks in advance.

"The API-version is not valid

I'm trying to make a PowerShell script, which will be run from a power-automate flow, and which in turn is supposed to call different power-automate flows via a Http request, the Url used here is generated by power-automate flow. My issue arise when I try to do
Invoke-Webrequest -Uri https://xxxx-xxx.westeurope.logic.azure.com:xxx/workflows/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/triggers/manual/paths/invoke?api-version=2016-06-01%26sp=%2Ftriggers%2Fmanual%2Frun%26sv=1.0%26sig=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxx -Method POST -Body $PostParams
And when this is run, I get the error of
The Api-Version '2016-06-01&sp=/triggers/manual/run&sv=1.0&sig=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx-rmZzrBfsAvjCc' is not valid. It goes on to suggest some valid ones, which among them are '2016-06-01'.
I have tried removing %26sp, which also did not work. I've tried searching up any information regarding Api-version is not valid, but yielded no result.
I think you should do Invoke-RestMethod
$request = 'https://put_here_your_uri'
$result = Invoke-RestMethod -Method Post -Uri $request
And if you have header like bearer token add -Headers $headers
$headers = #{
'Authorization' = 'bearer HERE_BEARER_TOKEN'
}
Turns out, after quite a bit of back and forth and with an external answer I found out the URL had to be encoded before I could use it in my code

How to include the "--include" curl tag when posting?

Pretty straightforward question. Posting with:
Invoke-WebRequest -Uri http://www.myWebsite.com/extension -Method POST -Body $myLocalFile
How can I add the "--include" cURL tag to this POST request so I get more information back from the server?
According to the cURL documentation, --include does the following:
(HTTP) Include the HTTP-header in the output. The HTTP-header includes things like server-name, date of the document, HTTP-version and more...
Invoke-WebRequest returns a HtmlWebResponseObject object, and one of the properties of the object is called Headers. That object is a hashtable containing the HTTP headers that came along with the HTTP response, and you can select that collection & look at the contents easily.
$Headers = Invoke-WebRequest -Uri http://www.myWebsite.com/extension -Method POST -Body $myLocalFile |
Select-object -expandproperty Headers
Or, if you need to capture the whole response & pick through it:
$Response = Invoke-WebRequest -Uri http://www.myWebsite.com/extension -Method POST -Body $myLocalFile;
$Response.headers;

Powershell Invoke-RestMethod POST Method timeout issue

I'm trying to write a program that deletes files from a specific device. The device has a REST API and I can access it from the CLI without any problems.
For example if I write this in the CLI, it works :
$clip="test.mov"
$ClipsURL="http://123.45.67.89/clips"
$ClipToDelete=#{action="delete";clipname=$clip}
Invoke-RestMethod -Uri $ClipsURL -Body $ClipToDelete -Method Post -ContentType "application/x-www-form-urlencoded"
I can play around with the $ClipToDelete parameter (changing the value of $clip) and it works every time.
Now when I put that in a loop (in my main script) it works the first time, and times out next.
foreach($clip in $ListClips) {
$clip="test.mov"
$ClipsURL="http://123.45.67.89/clips"
$ClipToDelete=#{action="delete";clipname=$clip}
Invoke-RestMethod -Uri $ClipsURL -Body $ClipToDelete -Method Post -ContentType "application/x-www-form-urlencoded"
}
While debuging I can clearly see that all the values passed as parameters to Invoke-RestMethod are correct (the URL & Body are correct).
My first impression is that I should probably close the session (if that makes any sense) before trying to Post again.
I tried adding a SessionVariable parameter to the command but it didnt change a thing
Does anyone already know how to close a web sesion left open (with the new Invoke-RestMethod command) ? Or does anyone think that the issue lies elsewhere ?
Thank you.
I had faced similar issues while invoking REST APIs from AirWatch.
Evidently the bug has been filed for POST & DELETE methods below:
https://connect.microsoft.com/PowerShell/feedback/details/836732/tcp-connection-hanging-in-close-wait-when-using-invoke-restmethod-with-put-or-delete
But I did face this issue intermittently with GET requests too, I documented the workarounds I had to use in the below TechNet Wiki article, Maybe you can try those and improve it if you find something extra :
http://social.technet.microsoft.com/wiki/contents/articles/29863.powershell-rest-api-invoke-restmethod-gotcha.aspx
foreach($clip in $ListClips) {
$clip="test.mov"
$ClipsURL="http://123.45.67.89/clips"
$ClipToDelete=#{action="delete";clipname=$clip}
Invoke-RestMethod -Uri $ClipsURL -Body $ClipToDelete -Method Post -ContentType "application/x-www-form-urlencoded"
}
In the second line you are overriding the current value with "text.mov".
If it's not a typo error, it could be that you delete the object the first time and you receive timeouts when trying to delete it again (it depends of course on the server-side implementation)