1

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.

Rock
  • 95
  • 1
  • 1
  • 9
  • The api key is part of the URL but you are trying to add it in the header of the request. – Nkosi Feb 02 '19 at 02:59

1 Answers1

0

You are doing a mistake placing the api key to DefaultRequestHeaders. Api key should be part of url. So instead of

client.DefaultRequestHeaders.Add("?apiKey=", ApiKey);

try

var builder = new UriBuilder("http://xyz123.com/public/node");
var query = HttpUtility.ParseQueryString(builder.Query);
query["apiKey"] = "12345";
builder.Query = query.ToString();
client.BaseAddress = new Uri(builder.ToString());

or use Handy extension methods used in this SO answer.

Karel Kral
  • 5,297
  • 6
  • 40
  • 50
  • Thanks for the quick reply. I am newbie to C#. I have worked with python and visual basic adequately. I am getting a syntax error for the line: var query = HttpUtility.ParseQueryString(builder.Query); Name 'HttpUtility' doesn't exist in current context. I have already used Systems.Web namespace. This line is under a statis async Task. How can I fix this? – Rock Feb 02 '19 at 03:25
  • Do you have referenced appropriate assembly? https://learn.microsoft.com/cs-cz/dotnet/api/system.web.httputility?view=netframework-4.7.2 – Karel Kral Feb 02 '19 at 03:52
  • System.web was missing in references. I added it. Based on your answer above, what should be the input argument to GetAsync call? `client.GetAsync(string url)` I have the following line of code: `HttpResponseMessage response = await client.GetAsync(url)` isn't the input argument same as client.BaseAddress? When this line is run, I do not get any output. It console output window says The thread 0x--- has exited with code 0 (0x0) – Rock Feb 02 '19 at 21:50
  • The common solution is using constant part of URL as the base address (http://xyz123.com/public) and the variable part (node?apiKey=xxx) as the parameter for GetAsync. – Karel Kral Feb 03 '19 at 17:32
  • Thanks. Can HttpClient.GetAsync(Uri) method used in a windows form application project? I create a new uri `Uri request_uri = new Uri("full path");` I pass this uri to HttpClient.GetAsync(Uri) method. `HttpResponseMessage response = await client.GetAsync(uri);` As soon as this line is executed, The windows form (GUI) pops up and I get thread 0x-- has exited with code 0 (0x0) in the console output window. I also get The program '[17800] ProjectName.exe' has exited with code -1 (0xffffffff). Other synchronous calls like `WebRequest.GetResponse()` seem to work just fine. What am I doing wrong? – Rock Feb 04 '19 at 06:13
  • The full path in my previous comment is: "http://xyz123.com/public/node?apiKey=1234" has http + : + // in the beginning – Rock Feb 04 '19 at 06:14
  • I found out why my windows form app freezes [here] (https://stackoverflow.com/questions/24316790/http-client-frozes-in-windows-form-app) Looks like a deadlock was created due to different thread type.Thanks for your previous help! – Rock Feb 04 '19 at 06:32
  • @Rock Ok, but why is this not accepted answer and not vote up? – Karel Kral Feb 04 '19 at 07:29
  • I have already accepted your original answer. Regarding upvote, it says that since I am a new member, my vote is recorded but not publicly displayed – Rock Feb 04 '19 at 14:04