EasyPythonJavaScriptTypeScript

Parse Cookie Header

Node.jsHTTPStrings

Parse an HTTP Cookie header string into an object.

solve('sid=abc; theme=dark'){ sid: 'abc', theme: 'dark' }.

Trim whitespace around names and values. Skip empty entries.

Sample tests

Test #1One cookie
Input: ["sid=abc"]
Output: {"sid":"abc"}
Test #2Three cookies
Input: ["a=1; b=2; c=3"]
Output: {"a":"1","b":"2","c":"3"}
Test #3Equals in value
Input: ["token=eyJhbGc=value"]
Output: {"token":"eyJhbGc=value"}
Test #4Extra whitespace
Input: [" a=1 ; b=2 "]
Output: {"a":"1","b":"2"}
Test #5Empty header
Input: [""]
Output: {}