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.