Using Powershell 5.1
I'm trying to post my body that includes a Boolean
$body= #{
"file"= $filePath
"name"= $fileName.name
"booleanValue" = $true
}
$response = Invoke-RestMethod -Uri $uri -Headers $headers -Method POST -body $body
The issue i'm having is the booleanValue not translating correctly, I have it working in my Postman test but I unable to get it working in my powershell.
I have tried 0 or 1 for it, 'true' and assigning a variable to [bool] but unable to handle it correctly in my typescript service.
Thanks for the help in advance,
Related
Trying to connect to a REST-API via Powershell client. When testing the endpoint in Postman, I have no problems at all. Here's the main part of the function (I have a [pscredential]$Creds parameter that I use to get the username and password):
[string]$username = $Creds.UserName
[string]$password = (New-Object System.Net.NetworkCredential($Creds.UserName, $Creds.Password, 'Null')).Password
[string]$authorizationInfo= ([Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(('{0}:{1}' -f $username, $password))))
Invoke-WebRequest -Uri "https://$($HostName)/api/" -Method Get -Headers #{Authorization = ('Basic {0}' -f $authorizationInfo)}
For some reason the Authorization header is different in my script than in Postman. I can even copy the Authorization header out of Postman and paste it into the -Headers parameter and everything works fine. I just don't see where I'm getting this wrong.
I can't tell you why that's not working, but I can suggest something that works for me all the time with APIs:
$auth = $username + ':' + $upassword
$Encoded = [System.Text.Encoding]::UTF8.GetBytes($auth)
$authorizationInfo = [System.Convert]::ToBase64String($Encoded)
$headers = #{"Authorization"="Basic $($authorizationInfo)"}
Invoke-WebRequest -Uri "https://$($HostName)/api/" -Method GET -Headers $headers
If that doesn't work, try this subtle difference with Invoke-Restmethod:
Invoke-RestMethod -Uri "https://$($HostName)/api/" -Method GET -Headers $headers
Working with APIs is always an adventure. Keep trying. :)
I want to pass a few body parameters using x-www-form-urlencoded format using powershell invoke-restmethod. Do not that this is working fine in PostMan. My code for this is below but is not working. How do I accomplish this in powershell?
$param = [System.Web.HttpUtility]::UrlEncode("channel:channelID
Content-Type:application/x-www-form-urlencoded
limit:50")
$uri="https://MySlackWebsite.com/api/channels.history"
$test2 = Invoke-RestMethod -Method POST -Uri $uri -Headers $headerJson -Body $param
I got this to work with the following using the guidance I got from #Erik Kalkoken.
$headerJson = #{Authorization="Bearer xoxp-xyz"}
$postParams = #{channel='roomID';limit=50}
$uri="https://slackserver.com/api/channels.history"
Invoke-RestMethod -Method POST -Uri $uri -Headers $headerJson -Body $postParams -ContentType "application/x-www-form-urlencoded"
Here is an example script on how to retrieve the list of messages from a channel with a POST request.
$postParams = #{token='xoxp-XXX';channel='C12345678'}
$test2 = Invoke-WebRequest -Uri https://slack.com/api/channels.history -Method POST -Body $postParams
Write-Host $test2
It tested and based on this answer about how to create a POST request with PowerShell.
https://www.ibm.com/support/knowledgecenter/en/SS4GSP_6.2.7/com.ibm.udeploy.api.doc/topics/udclient_addversionstatus.html
How to make this PUT call using Powershell please?. I am using Powershell 5.
I came across this post while trying to do the same thing. The problem for me was in knowing what exactly the correct URL was (see Adam Parsons' answer):
$URL = "url-goes-here"
After a lot of searching (IBM's documentation was not worth much in this effort), I was able to identify the correct URL by way of watching traffic in Chrome developer tools (thanks to Darrell Schrag's blog post: https://drschrag.wordpress.com/2013/10/03/the-udeploy-rest-api).
For those searching for this, my PowerShell REST call sequence now looks like this (and executes successfully):
$tokenEncoded = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes( "PasswordIsAuthToken:{"token":"$authToken"}" ))
$headers = #{Authorization = "Basic "+$tokenEncoded}
# 1. Get component version ID
$uri = "$uDeployServer:8443/cli/version/getVersionId`?component=$componentName&version=$versionName"
$versionId=Invoke-RestMethod -Uri $uri -Method GET -Headers $headers
# 2. Add component version status
$uri = "$uDeployServer:8443/rest/deploy/version/$versionId/status/$versionStatus"
Invoke-RestMethod -Uri $uri -Method PUT -Headers $headers
Probably something like this...
$Hash = #{
Component="StringValue"
Version="StringValue"
Status="StringValue"
}
$Json = $Hash | ConvertTo-Json
$URL = "url-goes-here"
$Cred = Get-Credential
Invoke-RestMethod -Method "POST" -Uri $url -Credential $Cred -Body $Json
$header = #{'Authorization'='Basic <auth code value>'}
$ping = Invoke-RestMethod -Uri "https://api.docparser.com/v1/ping" -Headers $header
ping works fine...returns "pong". I then make a request for the Parser ID which is needed for uploading documents. I am able to retrieve this value successfully.
$parser = Invoke-RestMethod -Uri "https://api.docparser.com/v1/parsers" -Headers $header
$parserID = $parser.id
Now here is where I try to upload a pdf, which fails.
$fileToParse = "C:\test.pdf"
$body = #{'file'=$fileToParse}
$uploadDoc = Invoke-RestMethod -Uri "https://api.docparser.com/v1/document/upload/$parserID" -Method Post -Headers $header -ContentType 'multipart/form-data' -Body $body
API response keeps saying "Error: input empty"
This is the documentation from Docparser on how to upload pdfs:
Any thoughts on what I'm doing wrong here? Thanks in advance,
Eric
The problem is that your body currently just contains the path to your local file. Docparser expects however the content of the file as multipart/form-data.
I never worked with PowerShell, but I think something like this should work for you:
$body = #{
"file" = Get-Content($fileToParse) -Raw
}
I got the code from this answer: How to send multipart/form-data with PowerShell Invoke-RestMethod
I have a powershell script and I am trying to assign the response of the GET method to value $a. Bellow I have my code but it does not work. How can I assign the response to this value?
Thank you
$a = Invoke-RestMethod -Uri "https://cloud.skytap.com/templates/555949" -Method GET -ContentType "application/json" -Headers $headers | out-null
Write-Host "$a"
You're sending your code to out-null, which removes the output!
Remove | Out-Null and you'll find that your assignment to $a is working. Or perhaps you'll find an error message instead. If so, let me know and I'll do my best to help you.
To clarify, you should be running this instead.
$a = Invoke-RestMethod -Uri "https://cloud.skytap.com/templates/555949" -Method GET -ContentType "application/json" -Headers $headers