diff --git a/mongoose.c b/mongoose.c
index 6e743303e3e3bddba4c0c6f7b990fe3597aa44f8..1b7e6cdfd9aa19766377460f966fafee9bf20275 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -1709,23 +1709,22 @@ int mg_get_var(const char *data, size_t data_len, const char *name,
   return len;
 }
 
-int mg_get_cookie(const struct mg_connection *conn, const char *cookie_name,
+int mg_get_cookie(const char *cookie_header, const char *var_name,
                   char *dst, size_t dst_size) {
   const char *s, *p, *end;
   int name_len, len = -1;
 
   if (dst == NULL || dst_size == 0) {
     len = -2;
-  } else if (cookie_name == NULL ||
-             (s = mg_get_header(conn, "Cookie")) == NULL) {
+  } else if (var_name == NULL || (s = cookie_header) == NULL) {
     len = -1;
     dst[0] = '\0';
   } else {
-    name_len = (int) strlen(cookie_name);
+    name_len = (int) strlen(var_name);
     end = s + strlen(s);
     dst[0] = '\0';
 
-    for (; (s = mg_strcasestr(s, cookie_name)) != NULL; s += name_len) {
+    for (; (s = mg_strcasestr(s, var_name)) != NULL; s += name_len) {
       if (s[name_len] == '=') {
         s += name_len + 1;
         if ((p = strchr(s, ' ')) == NULL)
@@ -1740,7 +1739,7 @@ int mg_get_cookie(const struct mg_connection *conn, const char *cookie_name,
           len = p - s;
           mg_strlcpy(dst, s, (size_t) len + 1);
         } else {
-          len = -2;
+          len = -3;
         }
         break;
       }
diff --git a/mongoose.h b/mongoose.h
index dee018b4f666d23b4593f426c4afcdca74417d48..d3ce2b7b860ee523859622cc53608f7e3fff51ef 100644
--- a/mongoose.h
+++ b/mongoose.h
@@ -285,8 +285,8 @@ int mg_get_var(const char *data, size_t data_len,
 //          parameter is not found).
 //      -2 (destination buffer is NULL, zero length or too small to hold the
 //          value).
-int mg_get_cookie(const struct mg_connection *,
-                  const char *cookie_name, char *buf, size_t buf_len);
+int mg_get_cookie(const char *cookie, const char *var_name,
+                  char *buf, size_t buf_len);
 
 
 // Download data from the remote web server.
diff --git a/test/unit_test.c b/test/unit_test.c
index 575cf37246264c81030406407b9f59d669bd52e9..be714db3433a2d73d1b5453ca11a060c5e5c2e2c 100644
--- a/test/unit_test.c
+++ b/test/unit_test.c
@@ -609,6 +609,22 @@ static void test_mg_strcasestr(void) {
   ASSERT(mg_strcasestr("aa", "AAB") == NULL);
 }
 
+static void test_mg_get_cookie(void) {
+  char buf[20];
+
+  ASSERT(mg_get_cookie("", "foo", NULL, sizeof(buf)) == -2);
+  ASSERT(mg_get_cookie("", "foo", buf, 0) == -2);
+  ASSERT(mg_get_cookie("", "foo", buf, sizeof(buf)) == -1);
+  ASSERT(mg_get_cookie("", NULL, buf, sizeof(buf)) == -1);
+  ASSERT(mg_get_cookie("a=1; b=2; c; d", "a", buf, sizeof(buf)) == 1);
+  ASSERT(strcmp(buf, "1") == 0);
+  ASSERT(mg_get_cookie("a=1; b=2; c; d", "b", buf, sizeof(buf)) == 1);
+  ASSERT(strcmp(buf, "2") == 0);
+  ASSERT(mg_get_cookie("a=1; b=123", "b", buf, sizeof(buf)) == 3);
+  ASSERT(strcmp(buf, "123") == 0);
+  ASSERT(mg_get_cookie("a=1; b=2; c; d", "c", buf, sizeof(buf)) == -1);
+}
+
 int __cdecl main(void) {
   test_mg_strcasestr();
   test_alloc_vprintf();
@@ -627,6 +643,7 @@ int __cdecl main(void) {
   test_request_replies();
   test_api_calls();
   test_url_decode();
+  test_mg_get_cookie();
 #ifdef USE_LUA
   test_lua();
 #endif