Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

HTTPS GET call using HttpClient

I am getting a 400 bad request trying to call an API using HttpClient and I am not exactly sure what I am missing.

I have tried the endpoint from my browser and from Postman and have not had any issues, but when I try to use the following code I get a 400 bad request:

using (HttpClient client = new HttpClient())
{
    var result = client.GetAsync("https://api.exchange.coinbase.com/products/ETH-USD/candles?granularity=300&start=2022-03-22&end=2022-03-23").Result;
}

Would anyone be able to tell me what I am missing to get this to work?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The request from the original code returns {"message":"User-Agent header is required."}. If it is the case, add user-agent header to fix the problem

using System;
using System.Net.Http;
using System.Net.Http.Headers;

using (HttpClient client = new HttpClient())
{   
    client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(".NET", "6.0"));
    var result = await client.GetAsync("https://api.exchange.coinbase.com/products/ETH-USD/candles?granularity=300&start=2022-03-22&end=2022-03-23");   
    Console.WriteLine(await result.Content.ReadAsStringAsync());
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading