Using HttpClient with the RightScale API Using HttpClient with the RightScale API powershell powershell

Using HttpClient with the RightScale API


I needed to add a "Authorization: Basic " header to my request.

In additional to the initial code I had posted:

HttpClient http = new HttpClient("https://my.rightscale.com/api/acct/accountid/login?api_version=1.0");
http.TransportSettings.UseDefaultCredentials = false;
http.TransportSettings.MaximumAutomaticRedirections = 0;
http.TransportSettings.Credentials = new NetworkCredential("username", "password");

I need to add the Authorization header along with the REST request with the username/password as follows:

byte[] authbytes = Encoding.ASCII.GetBytes(string.Concat("username",":", "password"));
string base64 = Convert.ToBase64String(authbytes);
string authorization = string.Concat("Authorization: Basic ", base64);
http.DefaultHeaders.Add(authorization);

And then when I made the request:

Console.WriteLine(http.Get().Content.ReadAsString());

I received the HTTP 204 along with the session cookie I was looking for. What can I say, Fiddler is awesome :) !