tengo authentatication básica trabajar con la API REST utilizando rizo:PowerShell HTTP Post REST API de autenticación básica
curl -X POST -H 'Accept: application/json' -u user:password http://localhost/test/
Pero, cuando trato de hacer lo mismo con WebRequest PowerShell, me sale 403 (permiso denegado). Esta secuencia de comandos funciona bien cuando deshabilito la comprobación de autenticación en el código REST.
¿Cuál es la mejor manera en powershell para pasar credenciales en la solicitud POST similar a curl o qué puedo hacer para corregir la siguiente secuencia de comandos.
Realmente agradecería alguna orientación sobre esto. Gracias.
Aquí está mi script de PowerShell:
function Execute-HTTPPostCommand() {
param(
[string] $target = $null
)
$username = "user"
$password = "pass"
$webRequest = [System.Net.WebRequest]::Create($target)
$webRequest.ContentType = "text/html"
$PostStr = [System.Text.Encoding]::UTF8.GetBytes($Post)
$webrequest.ContentLength = $PostStr.Length
$webRequest.ServicePoint.Expect100Continue = $false
$webRequest.Credentials = New-Object System.Net.NetworkCredential -ArgumentList $username, $password
$webRequest.PreAuthenticate = $true
$webRequest.Method = "POST"
$requestStream = $webRequest.GetRequestStream()
$requestStream.Write($PostStr, 0,$PostStr.length)
v$requestStream.Close()
[System.Net.WebResponse] $resp = $webRequest.GetResponse();
$rs = $resp.GetResponseStream();
[System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs;
[string] $results = $sr.ReadToEnd();
return $results;
}
$post = "volume=6001F930010310000195000200000000&arrayendpoint=2000001F930010A4&hostendpoint=100000051ED4469C&lun=2"
$URL = "http://example.com/test/"
Execute-HTTPPostCommand $URL
Gracias! Esto lo hizo por mí. –
Puede usar http://www.base64encode.org/ para obtener una cadena UTF-8 Base64. – SeriousM