multiple headers to the GET request

Anjali Agarwal 1,531 Reputation points
2024-09-06T05:42:45.45+00:00

How can I pass multiple headers to the GET request. below is my code. the response code is always BadReqest. Not sure what am I doing wrong..

var parameters = new PayParam
{
    token = "12345"
};
var json = JsonSerializer.Serialize(parameters);
using (HttpClient client = new HttpClient())
{
    client.BaseAddress = new Uri("https://test/test1");
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, client.BaseAddress);
    client.DefaultRequestHeaders.Add("ApiKey", "ABC12345");
    client.DefaultRequestHeaders.Add("MerchantCode", "Demo1");
    client.DefaultRequestHeaders.Add("MerchantKey", "Demo2");
    request.Content = new StringContent(json, Encoding.UTF8, "application/json");
    HttpResponseMessage response = await client.SendAsync(request);
    if (response.IsSuccessStatusCode)
    {
        var jsonString = await response.Content.ReadAsStringAsync();
        var data = JsonSerializer.Deserialize<PayParam>(jsonString);
        //Console.WriteLine($"tok: {data?.token}");
        string token = data?.token.ToString();
    }
    else
    {
        Console.WriteLine("Request failed: " + response.StatusCode);
    }

Any help will be highly appreciated.

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2024-09-06T06:22:14.31+00:00

    Hi @Anjali Agarwal , Welcome to Microsoft Q&A,

    In GET request, you should not add Content to the request, because GET request has no request body. In addition, the Uri parameter of HttpRequestMessage should be a relative path, not client.BaseAddress. You can set client.BaseAddress as the base URL and use the relative path as the path of HttpRequestMessage.

    var parameters = new PayParam
    {
        token = "12345"
    };
    var json = JsonSerializer.Serialize(parameters);
    
    using (HttpClient client = new HttpClient())
    {
        client.BaseAddress = new Uri("https://test/test1");
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Add("ApiKey", "ABC12345");
        client.DefaultRequestHeaders.Add("MerchantCode", "Demo1");
        client.DefaultRequestHeaders.Add("MerchantKey", "Demo2");
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, ""); 
        HttpResponseMessage response = await client.SendAsync(request);
    
        if (response.IsSuccessStatusCode)
        {
            var jsonString = await response.Content.ReadAsStringAsync();
            var data = JsonSerializer.Deserialize<PayParam>(jsonString);
            string token = data?.token.ToString();
        }
        else
        {
            Console.WriteLine("Request failed: " + response.StatusCode);
        }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.