- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to return an object by parsing http cookie header in JavaScript?
Cookies are small pieces of data that are sent from a website to a user's web browser. They are used to store information about the user, such as their preferences or login status.
When a user visits a website, their web browser will send a request to the server. The server will then send a response, which includes a set of headers. One of these headers is the "Cookie" header, which contains a list of all the cookies that are associated with the website.
Parsing the Cookie Header
In order to parse the Cookie header, we need to first split it into individual name-value pairs. This can be done using the String.split() method.
Once we have an array of name-value pairs, we can then loop through it and create an object. The object will have the cookie name as the key and the cookie value as the value.
Let's say we have the following Cookie header −
Set-Cookie: username=john; expires=Thu, 18 Dec 2019 12:00:00 GMT; path=/;
We can split it into individual name-value pairs like so −
var pairs = header.split(';');
This will give us the following array −
['username=john', 'expires=Thu, 18 Dec 2019 12:00:00 GMT', 'path=/']
We can then loop through this array and create an object −
var cookies = {}; for (var i = 0; i < pairs.length; i++) { var nameValue = pairs[i].split('='); cookies[nameValue[0].trim()] = nameValue[1]; }
This will give us the following object −
{ username: 'john', expires: 'Thu, 18 Dec 2019 12:00:00 GMT', path: '/' }
Approach
STEP 1 − We start by creating a function named parseCookieHeader(). This function will take the cookie header as a parameter.
STEP 2 − We use the String.split() method to split the header into individual name-value pairs.
STEP 3 − We create an empty object to store the cookies.
STEP 4 − We use a for loop to loop through all the name-value pairs
STEP 5 − We use the String.split() method to split each name-value pair into an array.
STEP 6 − We store the first element of the array (the cookie name) as the key in the cookies object. We store the second element of the array (the cookie value) as the value in the cookies object.
STEP 7 − We return the cookies object.
STEP 8 − We get the cookie header from the document.
STEP 9 − We call the parseCookieHeader() function and pass in the cookie header. This will return an object of all the cookies.
STEP 10 − We display the cookies in the console as well as on the output window.
Example
The following program demonstrates how to parse the HTTP Cookie header and return an object of all cookie name-value pairs. Here is the full working code −
<!Doctype HTML> <HTML> <head> <title>Example</title> </head> <body> <h3> Parsing HTTP Cookie Header </h3> <p> Object of all cookie name-value pairs:</p> <div id="result"></div> <script> // Function to parse the cookie header function parseCookieHeader(header) { // Split the header into individual name-value pairs var pairs = header.split(';'); // Create an object to store the cookies var cookies = {}; // Loop through the name-value pairs and store them in the object for (var i = 0; i < pairs.length; i++) { var nameValue = pairs[i].split('='); cookies[nameValue[0].trim()] = nameValue[1]; } // Return the object return cookies; } // Get the cookie header from the document var header = "username=john; expires=Thu, 18 Dec 2019 12:00:00 GMT; path=/;" // Parse the cookie header var cookies = parseCookieHeader(header); // Display the cookies console.log(cookies) document.getElementById("result").innerHTML = JSON.stringify(cookies) </script> </body> </html>
In this tutorial, we learned how to parse the Cookie header and return an object of all the cookie name-value pairs in JavaScript.
- Related Articles
- How to serialize a cookie name-value pair into a Set Cookie header string in JavaScript?
- How to read a HTTP header using JSP?
- How to return an object from a JavaScript function?
- Recursive string parsing into object - JavaScript
- How to “return an object” in C++?
- How to set a cookie and get a cookie with JavaScript?
- How to read a cookie using JavaScript?
- How to return the protocol (http or https) of the web page with JavaScript?
- Parsing array of objects inside an object using maps or forEach using JavaScript?
- How to return an array whose elements are the enumerable property values of an object in JavaScript?
- How to set cookie value with AJAX request in JavaScript?
- What is the maximum size of HTTP header values?
- Which methods can be used to read HTTP header in your JSP program.
- How to return an object from a function in Python?
- How to invert an object in JavaScript?
