0

I am trying to get data from this api codaBox I've tested it with swagger but when I am trying with I am getting a error code 401. I am using the right header, username and password.

I tested it with swagger with the same credentials and I've got data with it.

try
{
    var byteArray = Encoding.ASCII.GetBytes("Username:password");
    Client.DefaultRequestHeaders.Authorization =
        new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
    Client.BaseAddress = new Uri("https://myurl");

    Client.DefaultRequestHeaders.Add("X-Software-Company", "myToken");
    HttpResponseMessage response = await Client.GetAsync("");
    response.EnsureSuccessStatusCode();
    string responseBody = await response.Content.ReadAsStringAsync();
    // Above three lines can be replaced with new helper method below
    // string responseBody = await client.GetStringAsync(uri);

    Console.WriteLine(responseBody);
}
catch (HttpRequestException e)
{
    Console.WriteLine("\nException Caught!");
    Console.WriteLine("Message :{0} ", e.Message);
}

I would like to get 200 status code.

Matt
  • 1,245
  • 2
  • 17
  • 32
bob nkusi
  • 11
  • 2

1 Answers1

-1

You need to replace "Username:password" with the actual user and password separated by : "{your actual username}:{your actual password}"

Also Web is UTF8 (Ascii works sometimes as it's a subset) but not always:

var byteArray = Encoding.UTF8.GetBytes("Username:password");
Pop Catalin
  • 61,751
  • 23
  • 87
  • 115
  • of course i did it i just didn't want share my password and username all over the internet -) – bob nkusi Jul 08 '19 at 09:21
  • @bobnkusi I see, then see my edit. Try changing to UTF8 – Pop Catalin Jul 08 '19 at 09:26
  • 1
    The example you have, is the correct way to use Basic authentication. I think you need to double check that everything sent is correct. Try using Fillder or some other tool. – Pop Catalin Jul 08 '19 at 09:36
  • 1
    Or you can use a logging handler to see the exact request. See example here: https://stackoverflow.com/questions/18924996/logging-request-response-messages-when-using-httpclient – Pop Catalin Jul 08 '19 at 09:37