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.

1
  • The api key is part of the URL but you are trying to add it in the header of the request. Commented Feb 2, 2019 at 2:59

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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?
Do you have referenced appropriate assembly? learn.microsoft.com/cs-cz/dotnet/api/…
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 <url> 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)
The common solution is using constant part of URL as the base address (xyz123.com/public) and the variable part (node?apiKey=xxx) as the parameter for GetAsync.
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?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.