user1730289
user1730289

Reputation: 477

Teller.io, C#, Cannot connect to the API, but Curl works

I am trying to connect to TellerIO using C#, I have difficulties

Here is my snippet of code:

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, "https://api.teller.io/accounts");
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", "dGVzdF90b2tlbl9reTZpZ3lxaTNxeGE6Og==");
HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();

In Curl this works:

curl.exe https://api.teller.io/accounts -u test_token_ky6igyqi3qxa4: -v

What am missing?

Thank you

Upvotes: 0

Views: 52

Answers (1)

It all makes cents
It all makes cents

Reputation: 5009

For a project using .NET 8, download/install the NuGet package System.Net.Http

Then try the following:

Note: The code below was adapted from here and the Teller documentation.

Add the following using directives

  • using System.Net.Http.Headers;
  • using System.Diagnostics;
public async Task Test()
{
    using (HttpClient client = new HttpClient())
    {
        string baseUrl = "https://api.teller.io";
        client.BaseAddress = new Uri(baseUrl);

        //convert string to byte[]
        byte[] byteArray = UTF8Encoding.UTF8.GetBytes("test_token_ky6igyqi3qxa4:");
        Debug.WriteLine($"Authorization: Basic {Convert.ToBase64String(byteArray)}");

        //set Authorization header value as base64 string
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

        //option 1:
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "/accounts");
        HttpResponseMessage response = await client.SendAsync(request);
        Debug.WriteLine($"response: {response}");

        //option 2:
        //HttpResponseMessage response = await client.GetAsync($"{baseUrl}/accounts");
        //Debug.WriteLine($"response: {response}");
    }
}

Additional Resources:

Upvotes: 1

Related Questions