Search code examples
javascriptcookiesjs-of-ocaml

Js_of_ocaml - Error when get cookie


I'm XmlHttpRequest in order to make http request and I want to get the cookies. The code to get cookie :

let http_post url =
  XmlHttpRequest.perform_raw_url
    ~post_args:[("login", `String (Js.string "foo"));
                ("password", `String (Js.string "bar"))]
    url >>= fun r -> 
  let code = r.XmlHttpRequest.code in
  let msg = r.XmlHttpRequest.content in
  let cookie = match r.XmlHttpRequest.headers "Set-Cookie" with
  | None -> "Empty Cookie"
  | Some s -> s in 
  if code = 0 || code = 200
  then Lwt.return (msg,cookie)

let make_test_request id =
  let button = get_element_by_id id in
  button##onclick <- (Html.handler (fun _ ->
      http_post "www.website.com" >>=
      (fun (msg,cookie) ->
         Printf.printf "cookie = %s\n" cookie;
         Html.document##cookie <- Js.string cookie;
         Printf.printf "s = %s\n" msg;
         Lwt.return());
      Js._true))

The cookies should be in the headers and I'm getting this error : Refused to get unsafe header "Set-Cookie"

It is the way that I'm getting the cookies wrong or a problem with my web browser (I'm using chromium) ?


Solution

  • http://www.w3.org/TR/XMLHttpRequest/#the-getresponseheader()-method

    client . getResponseHeader(header) Returns the header field value from the response of which the field name matches header, unless the field name is Set-Cookie or Set-Cookie2.

    answers at Why cookies and set-cookie headers can't be set while making xmlhttprequest using setRequestHeader?