I am working on a powershell script to post json data to a REST interface and am getting (400) Bad Request on each time. I am new to this, and am unclear as to if/how I should be encoding the data. I know I need to set the contenttype to application/json, but is the encoding choice I am using what is causing my problem, and if so what should I be using?
$cred = New-Object System.Net.NetworkCredential -ArgumentList $authUser,$authPass
$url = 'http://localhost:8080/alfresco/service/api/people'
$request = [Net.WebRequest]::Create($url)
$request.ServicePoint.Expect100Continue = $false
$request.Credentials = $cred
$request.ContentType = "application/json"
$request.Method = "POST"
$data = (New-Object PSObject |
Add-Member -PassThru NoteProperty username $username |
Add-Member -PassThru NoteProperty firstName $firstname |
Add-Member -PassThru NoteProperty lastName $lastname |
Add-Member -PassThru NoteProperty email $email |
Add-Member -PassThru NoteProperty password $password
) | ConvertTo-JSON
$bytes = [System.Text.Encoding]::ASCII.GetBytes($data)
$request.ContentLength = $bytes.Length
$requestStream = [System.IO.Stream]$request.GetRequestStream()
$requestStream.write($bytes, 0, $bytes.Length)
$response = $request.GetResponse()