Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell http post REST API basic authentication

I have basic authentatication working with REST API using curl:

curl -X POST  -H 'Accept: application/json' -u user:password http://localhost/test/

But, when I try to do the same with powershell webRequest, I get 403(permission denied). This script works fine when I disable authentication check in REST code.

What is the best way in powershell to pass credentials on POST request similar to curl or what can I do to fix following script.

Would really appreciate some guidance on this. Thanks.

Here is my powershell script:

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
like image 694
R D Avatar asked Jan 19 '12 00:01

R D


People also ask

How can I pass the basic HTTP authentication?

We can do HTTP basic authentication URL with @ in password. We have to pass the credentials appended with the URL. The username and password must be added with the format − https://username:password@URL.

How do I use REST API in PowerShell?

To call a REST API from the Windows PowerShell, you should use the Invoke-RestMethod cmdlet. A call to an API is simply a request through HTTP or HTTPS. So, you will need a URL to which the API will be sent. You can find detailed information about the URL to call to get data from API documentation.


3 Answers

I know this is an old thread, but for those who might stumble across this the invoke-restmethod is a much better, and simpler, vehicle for making API calls with PowerShell.

Build a parameter list as a hash table:

$params = @{uri = 'https:/api.trello.com/1/TheRestOfYourURIpath';
                   Method = 'Get'; #(or POST, or whatever)
                   Headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($acctname):$($password)"));
           } #end headers hash table
   } #end $params hash table

$var = invoke-restmethod @params

Your parameter hash table may differ slightly.

I actually haven't gotten this to work with Trello, but I have with GitHub, Serena Business Manager and Jira.

like image 50
Brian Bagent Avatar answered Oct 07 '22 06:10

Brian Bagent


Your code looks good, I would try adding HTTP_AUTHORIZATION header for $webrequest like this:

$webRequest.Headers.Add("AUTHORIZATION", "Basic YTph");

Where YTph would be the base64encoded string for username : password.

like image 41
Raj J Avatar answered Oct 07 '22 08:10

Raj J


Credentials property seems to be used for Windows authentication. Try using this function: Forcing Basic Authentication in WebRequest I would advise you in any case to use some web debugger, like Fiddler to see the difference between curl request and your request

like image 33
Andrey Marchuk Avatar answered Oct 07 '22 06:10

Andrey Marchuk