Cant get a response from API Rest call with Powershell script - powershell

Im trying to send an API call to X and then the response will trigger some other things.
The issue i have is that im not getting any response in the ISE.
When im sending manually with fiddler/postman i get a response.
Im sending a simple post :
$Json = '{"Headers_In_Here"}}'
$Post = Invoke-WebRequest 'http://Server_URL' -Method Post -Body $Json -ContentType 'application/json'

Try adding ConvertTo-Json .
$Post = Invoke-WebRequest 'http://Server_URL' -Method Post -Body $Json -ContentType 'application/json' | ConvertTo-Json

Related

Invoke-RestMethod to acces Joe SandBox API

I'm trying to create a powershell script to access JoeSandBox API to download reports.
I'm following their API details there https://www.joesandbox.com/userguide?sphinxurl=usage/webapi.html
Here's the beginning of the REST script I've put together:
$json = #{
apikey="XXXX";
webid= "YYYY"
} | ConvertTo-Json
invoke-restmethod -uri 'https://www.joesandbox.com/api/v2/analysis/download' -Method POST -Body $json -ContentType 'multipart/form-data'
Basically, I don't get the access...
Invoke-RestMethod : {"errors":[{"code":2,"message":"apikey is required."}]}
Thanks
$body = #{'apikey'='XXX'; 'webid'='YYY'}
invoke-restmethod -uri 'https://www.joesandbox.com/api/v2/analysis/download' -Method POST -body $body

Invoke-RestMethod : Could not find authentication data on request when send to headers

I added powershell script in azure devops release and try to send POST request using Invoke-WebRequest with Authorization token and xml file.
What am I doing wrong?
I get Authorization token from another POST request and tried to create headers with this token and trying send xml file using x-ray endpoint (NUnit XML results - POST /api/v1/import/execution/nunit)
https://confluence.xpand-it.com/display/XRAYCLOUD/Import+Execution+Results+-+REST#ImportExecutionResults-REST-NUnitXMLresults
Authorization token like headers parameters
Content-type like powershell parameters
$bodyForAuth=#{"client_id"="...";
"client_secret"="...";
}
$jsonAuth=Invoke-WebRequest -Uri $urlForAuth -Method POST -Body ($bodyForAuth|ConvertTo-Json) -ContentType "application/json"
$authToken=ConvertFrom-Json -InputObject $jsonAuth
$headers=#{ Authorization = "Bearer $authToken" }
Invoke-WebRequest -Headers $headers -Uri $urlForXrayApi -Method POST -ContentType "text/xml" -InFile $TestResultsXmlPath
And I have error: "Invoke-RestMethod : Could not find authentication data on request"
Also I try to send like this:
$headers=#{ Authorization = "Bearer $authToken";"Content-Type"="text/xml" }
Invoke-WebRequest -Headers $headers -Uri $urlForXrayApi -Method POST -InFile $TestResultsXmlPath
And also have the same error : "Invoke-RestMethod : Could not find authentication data on request"
But if I send POST request like this (without Content-Type):
$bodyForAuth=#{"client_id"="...";
"client_secret"="...";
}
$jsonAuth=Invoke-WebRequest -Uri $urlForAuth -Method POST -Body ($bodyForAuth|ConvertTo-Json) -ContentType "application/json"
$authToken=ConvertFrom-Json -InputObject $jsonAuth
$headers=#{ Authorization = "Bearer $authToken" }
Invoke-WebRequest -Headers $headers -Uri $urlForXrayApi -Method POST -InFile $TestResultsXmlPath
I have SERVER ERROR: "Invoke-RestMethod : {"error":"Missing data in the nunit results import request"}"
Power Shell v. 5.1
Why when I send to headers server can not fine authorization data?
I needed parenthesis, so I changed $TestResultsXmlPath to $(TestResultsXmlPath)
Invoke-WebRequest `
-Headers $headers `
-Uri $urlForXrayApi `
-Method POST `
-InFile $(TestResultsXmlPath)

PowerShell - Slack API - Room History - Post method with x-www-form-urlencoded parameters

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.

How pass API Key to the Azure Machine Learning Web service using PowerShell?

I am trying to connect to Azure Machine Learning Web service using Invoke-WebRequest in PowerShell. after bellow command I will get an error that "Request is unauthorized to access
resource.":
Invoke-WebRequest -Uri $Url -Method POST -Body $body
As I know, you can connect to a Machine Learning Web service using any programming language that supports HTTP request and response. read more about it here.
Seems I need to pass API Key with my request. I have tried this two types of command, but the error was same:
Invoke-WebRequest -Uri $Url -Method POST -Body $body -Headers #{'apikey' = $API_key}
and
Invoke-WebRequest -Uri $Url -Method POST -Body $body -Header #{ "X-ApiKey" = $API_key }
Can you please guide me how I can pass API Key to the Azure Machine Learning Web service using PowerShell?
Per TheIncorrigible's comment, try this:
Invoke-WebRequest -Uri $Url -Method POST -Body $body -Headers #{ Authorization = "Bearer " + $API_key }
You are passing a JSON string, so you could also just use the ConvertTo-Json command to create your true API key. For info on that check this out: using powershell with JSON data
You should use this:
Invoke-WebRequest -Uri $Url -Method POST -Body $body -Headers #{ 'Content-Type' = 'application/json'; 'Authorization' = "Bearer " + $API_key }

Powershell: Uploading file to Docparser API using Invoke-RestMethod

$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