Skip to content

TypeError from fetch module due to no headers received #1737

Closed

Description

@wubzz

This is part bug report, and part question.

Bug report
I don't know if you would classify this as a bug, but when a request done via the fetch module returns no headers, it will throw an error here:

https://github.com/NativeScript/NativeScript/blob/master/xhr/xhr.ts#L119

Due to the returnvalue here:
https://github.com/NativeScript/NativeScript/blob/master/xhr/xhr.ts#L184
(toLowerCase on null)

Which eventually calls the xhr.onerror handler which is set to always throw:

reject(new TypeError('Network request failed'))

Headers should be set, but when they're not, this is what happens. Which leads me to my question...

Question
I'm completely stumped by this and am by no means implying that this is caused by the fetch/xhr modules, but I'm completely lost on where to look right now. When my remote API endpoint returns with a 401 - Unathorized, the xhr response does not pick up any response headers. If I return a 403 instead, response headers are present.

I've verified that it's not the API server doing something wrong, because the response headers are picked up by numerous other Clients such as Postman, Webbrowser, etc, when I return a 401. Any ideas?

Running all this on Android.

JS: Response {
JS:   "content": {
JS:    .......
JS:   },
JS:   "statusCode": 401,
JS:   "headers": {}
JS: }
JS: Response {
JS:   "content": {
JS:    .......
JS:   },
JS:   "statusCode": 403,
JS:   "headers": {
JS:     "Date": "Thu, 10 Mar 2016 14:32:10 GMT",
JS:     "Content-Type": "application/json; charset=utf-8",
JS:     "Content-Length": "286",
JS:     "Keep-Alive": "timeout=15, max=100",
JS:     "Connection": "Keep-Alive",
JS:     "X-Android-Sent-Millis": "1457620330281",
JS:     "X-Android-Received-Millis": "1457620330364"
JS:   }
JS: }

Activity

  1. self-assigned this
    on Mar 11, 2016
  2. enchev commented on Mar 11, 2016

    @enchev
    Contributor

    Hey @wubzz,

    The null ref exception was fixed immediately (check my pull request) and here is an example how to get the headers:

        fetch("http://httpbin.org/status/401").then(function(r){
            console.log("STATUS: " + r.status);
    
            r.headers.forEach(function(key, value){
                console.log(key + ":" + value);
            });
    
            return r.text();
        }, function(e){
            console.log("ERROR: " + e)
        }).then(function(r){
            console.log("CONTENT: " + r)
        });
  3. added this to the 1.7.0 milestone on Mar 11, 2016
  4. wubzz commented on Mar 11, 2016

    @wubzz
    Author

    @enchev Thanks!

    Debugging this on my own by logging the output of the xhr module, I still find no explanation why the headers object was empty to begin with. It makes no sense at all. Even here:
    https://github.com/NativeScript/NativeScript/blob/master/xhr/xhr.ts#L85
    There are no headers set in the response. I got stuck after that and couldn't dig deeper (http.request(..)), so I'll just add this info here in case someone else has any idea.

  5. enchev commented on Mar 12, 2016

    @enchev
    Contributor

    Hey @wubzz,

    After I've fixed the exception I was able to get headers using http://httpbin.org/status/401 url from the echo service we use for testing.

  6. bismert commented on Jun 23, 2017

    @bismert

    I'm getting similar error when trying to make post request with fetch module
    CLI error prints
    TypeError: Network request failed: unexpected expected token < in JSON at position 3

  7. lock commented on Aug 28, 2019

    @lock

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  8. locked and limited conversation to collaborators on Aug 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions