I am building C# REST Client in windows form application project in VS 2017.
I have been following an excellent example from Microsoft documentation here
I am trying to implement HTTP GET method to obtain a json data from a server. The server uses an api key for authentication and has to be included in the GET request path or url as follows:
"xyz.com/node?apiKey=12345"
I have tried to add this authentication header as follows:
static async Task RunAsync()
{
string ApiKey = "12345";
string baseAddr = "http://xyz123.com/public/node";
client.BaseAddress = new Uri(baseAddr);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("?apiKey=", ApiKey);
client.DefaultRequestHeaders.Accept.Add(
new
System.Net.Http.Headers.MediaTypeWithQualityHeaderValue
("application/json"));
// get node serial numbers
try
{
Although VS is not showing me any errors, I feel that there is mistake in which authentication header is added to include api key. Particularly in the line:
client.DefaultRequestHeaders.Add("?apiKey=", ApiKey);
No json object is being returned. The thread just terminates without any output.