Damian
Damian

Reputation: 3

transfer xml file to a URL using powershell script

I have an xml document that needs to be uploaded to a url for processing by the webservice, I have limited resources so I'll have to use a powershell script for this task, ver 1. No, I cannot upgrade to 3 which I know has more tools for this type of job.

Basically I need to replicate this (Which works fine in linux)

curl -d @event1.xml URL -H 'Content-Type: text/xml'  -s

So far all I've been able to find is that I need to use System.Net.HttpWebRequest but using this is beyond me since I haven't been able to use it to get a response.

SO, is there a way to upload a text file using powershell? Or will I have to go about this another way completely?

Upvotes: 0

Views: 2969

Answers (1)

Frode F.
Frode F.

Reputation: 54971

I don't have a site to test with, but I guess you need something like this:

$req = New-Object System.Net.HttpWebRequest
$req.Method = "POST"
$req.ContentType = "text/xml"
$data = [System.IO.File]::ReadAllBytes("C:\test.xml")
$req.ContentLength = $data.Length
$reqstream = $req.GetRequestStream()
$reqstream.Write($data, 0, $data.Length)
$reqstream.Close()

If you need to catch the response, you will have to add that.

$res = $req.getresponse().getresponsestream() 

++

Upvotes: 2

Related Questions