Hello guys, In this article, we will learn about What is response headers in .net core with an example.
There are four different types of headers:
- General Header
- Request Header
- Response Header
- Entity Header
What Is General Header?
- This type of header is applied to both Request and Response headers both but without affecting the database body.
- It is a general-type header used to pass additional information with an HTTP response or HTTP request.
What Is a request header?
- A request header is an HTTP header that can be included in an HTTP request to offer information about the request context to the server, allowing it to personalize the answer.
- This type of header contains information about the fetched request by the client.
What Is Response Header?
- HTTP (Hypertext Transfer Protocol) headers are used to communicate additional information between clients and servers.
- A colon separates all header fields, and key-value pairs are recorded in clear-text string format. The end of the header section is indicated by an empty field header.
What Is Entity Header?
- This type of header contains the information about the body of the resources like MIME type and Content-length.
- It is used for the media type of the resource. The media type is a string sent along with the file to indicate the format of the file.
Let’s create a simple method to get and set Response Headers.
[Route("setandandheaders")] [HttpGet] public IActionResult SetAndGetResponse() { var headers = new List<dynamic>(); Response.Headers.Add("Name", "Parth Mandaliya"); Response.Headers.Add("City", "Surat"); foreach (var item in Response.Headers) { headers.Add($"{item.Key} : {item.Value}"); } return Ok(headers); }
Output:
Now we get the Response Headers in jQuery.
function GetResponseHeaders() { $.ajax({ type: 'GET', url: 'API URL', success: function (data, textStatus, request) { alert(request.getAllResponseHeaders()); }, error: function (request, textStatus, errorThrown) { alert(request.getResponseHeader('some_header')); } }); }
Output:
Conclusion
In this article, we have learned about What is Response Header and created examples in .net core and jQuery.
Thank you.