Get custom header value from IRestResponse in ASP.Net

on Aug 02, 2022 12:23 AM


If you send a request through Postman and check the response, there is a tab called Headers.

That's what I need to retrieve. For debugging, I want to get a custom header called TransactionID. I want to get the header with the response I'm receiving from the server.

This is my code and I want to save the headers of the response.

How can I do this?

var client = new RestClient(ConfigurationManager.AppSettings["base_url"]);
client.Timeout = -1;
var request = new RestRequest(Method.POST);
var authenticationString = string.Format("{0}:{1}", ConfigurationManager.AppSettings["User"], ConfigurationManager.AppSettings["Password"]);
var base64EncodedAuthenticationString = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(authenticationString));
request.AddHeader("Authorization", "Basic " + base64EncodedAuthenticationString);
request.AddHeader("Content-Type", "application/xml");
var body = @"<Testing>"
                + "\n" + @"    <var1>var</var1>"
                + "\n" + @"</Testing>";
request.AddParameter("application/xml", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);


Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Aug 03, 2022 12:39 AM

I found the solution I was looking I'm posting it below

foreach (var header in response.Headers)
    Console.WriteLine(header.Name+": "+header.Value);