11

In Windows PowerShell 3.0 was introduced Invoke-RestMethod cmdlet.

Invoke-RestMethod cmdlet accepts -Body<Object> parameter for setting the body of the request.

Due to a certain limitations Invoke-RestMethod cmdlet could not be used in our case. From the other hand, an alternative solution described in article InvokeRestMethod for the Rest of Us suits our needs:

$request = [System.Net.WebRequest]::Create($url)
$request.Method="Get"
$response = $request.GetResponse()
$requestStream = $response.GetResponseStream()
$readStream = New-Object System.IO.StreamReader $requestStream
$data=$readStream.ReadToEnd()
if($response.ContentType -match "application/xml") {
    $results = [xml]$data
} elseif($response.ContentType -match "application/json") {
    $results = $data | ConvertFrom-Json
} else {
    try {
        $results = [xml]$data
    } catch {
        $results = $data | ConvertFrom-Json
    }
}
$results 

But it is intended for a GET method only. Could you please suggest how to extend this code sample with the ability to send the body of the request using POST method (similar to Body parameter in Invoke-RestMethod)?

2 Answers 2

20

First, change the line that updates the HTTP method.

$request.Method= 'POST';

Next, you need to add the message body to the HttpWebRequest object. To do that, you need to grab a reference to the request stream, and then add data to it.

$Body = [byte[]][char[]]'asdf';
$Request = [System.Net.HttpWebRequest]::CreateHttp('http://www.mywebservicethatiwanttoquery.com/');
$Request.Method = 'POST';
$Stream = $Request.GetRequestStream();
$Stream.Write($Body, 0, $Body.Length);
$Request.GetResponse();

NOTE: PowerShell Core edition is now open source on GitHub, and cross-platform on Linux, Mac, and Windows. Any issues with the Invoke-RestMethod cmdlet should be reported on the GitHub issue tracker for this project, so they can be tracked and fixed.

Sign up to request clarification or add additional context in comments.

7 Comments

thank you, Trevor! This is the way how i thought it should be implemented but was not sure that this is the best way
@TrevorSullivan How the body would look like if i have a json on it?
@campinho: Simply replace the "asdf" with your JSON. :)
@TrevorSullivan can I send a file this way? Like $Body = [reports[]]$filecontent; I got a question here and now I come over this answer. How do you think, this solution will work in my case?
@irl_irl Yeah, I got the answer to this question here
|
5
$myID = 666;
#the xml body should begin on column 1 no indentation.
$reqBody = @"
<?xml version="1.0" encoding="UTF-8"?>
<ns1:MyRequest
    xmlns:ns1="urn:com:foo:bar:v1"
    xmlns:ns2="urn:com:foo:xyz:v1"
    <ns2:MyID>$myID</ns2:MyID>
</ns13:MyRequest>
"@

Write-Host $reqBody;

try
{
    $endPoint = "http://myhost:80/myUri"
    Write-Host ("Querying "+$endPoint)
    $wr = [System.Net.HttpWebRequest]::Create($endPoint)
    $wr.Method= 'POST';
    $wr.ContentType="application/xml";
    $Body = [byte[]][char[]]$reqBody;
    $wr.Timeout = 10000;

    $Stream = $wr.GetRequestStream();

    $Stream.Write($Body, 0, $Body.Length);

    $Stream.Flush();
    $Stream.Close();

    $resp = $wr.GetResponse().GetResponseStream()

    $sr = New-Object System.IO.StreamReader($resp) 

    $respTxt = $sr.ReadToEnd()

    [System.Xml.XmlDocument] $result = $respTxt
    [String] $rs = $result.DocumentElement.OuterXml
    Write-Host "$($rs)";
}
catch
{
    $errorStatus = "Exception Message: " + $_.Exception.Message;
    Write-Host $errorStatus;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.