Is there any javascript function, to parse the first line of the http header?
GET /page/?id=173&sessid=mk9sa774 HTTP/1.1
The url is encoded.
I would like to get an object, like this:
{
"method" : "GET",
"url" : "/page/",
"parameters": {
"id" : 173,
"sessid" : "mk9sa774"
}
}
I searched a lot, but I haven’t found anything useful.
thanks in advance,
First you can split on spaces:
Now you can get the method, unparsed path, and version:
Then you can split up the path into the query string and non-query string parts:
If there is a query string, we can then split it up into
key=valuestrings:Then we can unescape them and stuff them into an object:
If you really wanted to, you could then put all that data into an object:
If you’re in a browser environment, that’s the only way to do it. If you’re using Node.JS, it can deal with the URL parsing for you.