9

I am having problems when using together Cordova and fetch API. I am executing the following code

fetch(BASE_URL + '/auth/login', {
  method: 'post',
  credentials: 'include',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  body: transformRequest({username: email, password: password})
}).then(response => {
      console.log(response.headers.get('X-AuthToken'))
});

When the code is executed in the browser the 'X-AuthToken' header is correctly retrieved and logged. When I run the same code when packaged in my Cordova app the 'X-AuthToken' header is null. Moreover what is strange is that i can see perfectly the header set when checking the response server side and when sniffing on the network so I am completely sure the header is there (simply it is not returned by the fetch API); in fact when using the equivalent XMLHttpRqeuest the header is correctly set:

var xhttp = new XMLHttpRequest();
xhttp.open("POST", BASE_URL + /api/auth/login", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("username=username&password=password");
xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
       console.log (xhttp.getResponseHeader('X-AuthToken'));
    }
}

It is worth signaling that when I try to dump other common headers like pragma, cache-control, ... they are correctly logged. It seams like the fetch API is filtering the headers and removing the ones that are not standard. Is someone else experiencing the same problem? Am I missing something?

2
  • 1
    the fetch api has not a good support on mobile browsers. caniuse.com/#feat=fetch
    – albanx
    Mar 2, 2016 at 11:51
  • 1
    @pinturic android and ios cordova projects both use webkit. Fetch API is not 100% supported in webkit yet. see my answer below
    – tnt-rox
    Mar 3, 2016 at 14:25

1 Answer 1

1

Nicely put question, you are developing on the cutting edge. I would stick to XMLHTTPRequest for now. Fetch api is not properly implemented in webkit. See webkit bugzilla bug 151937

2
  • How about in 2021?
    – Luke
    Sep 1, 2021 at 15:58
  • Or how about in 2023?
    – goleon
    Feb 9 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.