I am using the following powershell script to send xml data stored in a text file (soap.txt) to an endpoint.
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("charset", 'utf-8')
$headers.Add("SOAPAction", 'nms:rtEvent#PushEvent')
Invoke-WebRequest http://localhost:8080/nl/jsp/soaprouter.jsp -Method Post -ContentType 'text/xml' -Headers $headers -InFile D:\Scripts\archive\soap.txt
Whilst the above method works fine, I wish to use the -Body param instead so that I can enrich the content but I am facing some issues, below is the code.
## Set SOAP headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("charset", 'utf-8')
$headers.Add("SOAPAction", 'nms:rtEvent#PushEvent')
$url = "http://localhost:8080/nl/jsp/soaprouter.jsp"
$body = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:nms:rtEvent">
<soapenv:Header/>
<soapenv:Body>
...etc etc
</soapenv:Body>
</soapenv:Envelope>'
Invoke-WebRequest -Method Post -Uri $url -Headers $headers -Body $body -ContentType 'application/xml'
I am getting the following error
I've also tried
$body = #"
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:nms:rtEvent">
<soapenv:Header/>
<soapenv:Body>
...rest of code here asd
</soapenv:Body>
</soapenv:Envelope>
"#
Invoke-WebRequest -Method Post -Uri $url -ContentType 'text/xml' -Headers $headers -Body $body
And i get that the header must be modified using the appropiate property or method, so I append the content type also to the header but that doesn't work either, I am using powershell 3.0
The answer is to remove the XML indentation in the variable, is this issue documented somewhere? or is it a limitation.
$body = #"
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:nms:rtEvent">
<soapenv:Header/>
<soapenv:Body>
...rest of code here asd
</soapenv:Body>
</soapenv:Envelope>
"#
Related
I have a web server that responds to a request https://localhost/GetUpdateInfo with a body [{"ipAddress": "10.200.2.55"}]
In postman its Work but in powershell i can`t do this because body begins from array.
When i do exmple code:
$Url = "https://localhost/GetUpdateInfo"
$Body = #(#{
ipAddress = "10.200.2.55"
})
$jsonBody = ConvertTo-Json -InputObject $Body
Invoke-RestMethod -Method 'Post' -Uri $url -Body $jsonBody
i hve error: Invoke-RestMethod : Block length does not match with its complement.
The content type of all POST requests is application/x-www-form-urlencoded unless specified.
Add -ContentType application/json to your last line call so your json is sent correctly.
Invoke-RestMethod -Method 'Post' -Uri $url -Body $jsonBody -ContentType 'application/json'
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
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)
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.
I'm trying to use the Invoke-RestMethod cmdlet in PowerShell 3 and 4, to upload a large binary file using a REST API's multipart/form-data upload. Here is a working cURL example on how to perform what I want to do in PowerShell:
curl -i -k -H "accept: application/json" -H "content-type: multipart/form-data" -H "accept-language: en-us" -H "auth: tokenid" -F file="#Z:\large_binary_file.bin" -X POST "https://server/rest/uri2"
I would love to see a working example on how to use Invoke-RestMethod to POST a multipart/form-data. I found a blog post from the PowerShell team showing how to use Invoke-RestMethod to upload to OneDrive (aka SkyDrive), but doesn't work well. I'd also like to avoid using System.Net.WebClient if at all possible. I also found another thread here on Stackoverflow, but it really didn't help much.
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
$server = "https://server"
uri = "/rest/uri1"
$headers = #{"accept" = "application/json"; "content-type" = "application/json";"accept-language" = "en-us"}
$body = #{"userName" = "administrator"; "password" = "password"}
$method = "POST"
#Get Session ID
$resp = Invoke-RestMethod -Method $method -Headers $headers -Uri ($server+$uri) -body (convertto-json $Body -depth 99)
$sessionID = $resp.sessionID
#Upload file
$uri = "/rest/uri2"
$headers = #{"accept" = "application/json";"content-type" = "multipart/form-data"; "accept- language" = "en-us"; "auth" = $sessionID}
$medthod = "POST"
$largeFile = "Z:\large_binary_file.bin"
I have tried both ways of using Invoke-RestMethod:
Invoke-RestMethod -Method $method -Headers $headers -Uri ($server+$uri) -InFile $largeFile
or
$body = "file=$(get-content $updateFile -Enc Byte -raw)"
Invoke-RestMethod -Method $method -Headers $headers -Uri ($server+$uri) -body $body
I notice couple of mistakes in your invoke statement. First, you need to use POST keyword instead of string value. Second, make sure that Content-Type is set to multipart/form-data. So this is my revised statement -
$uri = "http://blahblah.com"
$imagePath = "c:/justarandompic.jpg"
$upload= Invoke-RestMethod -Uri $uri -Method Post -InFile $imagePath -ContentType 'multipart/form-data'
You can check the response from the server by checking $upload.