{"action":"created","issue":{"url":"https://api.github.com/repos/request/request/issues/1951","labels_url":"https://api.github.com/repos/request/request/issues/1951/labels{/name}","comments_url":"https://api.github.com/repos/request/request/issues/1951/comments","events_url":"https://api.github.com/repos/request/request/issues/1951/events","html_url":"https://github.com/request/request/issues/1951","id":121000387,"number":1951,"title":"Safari: Empty Bearer Authorization header causes error in Safari (Desktop/iOS)","user":{"login":"nemothekid","id":940094,"avatar_url":"https://avatars.githubusercontent.com/u/940094?v=3","gravatar_id":"","url":"https://api.github.com/users/nemothekid","html_url":"https://github.com/nemothekid","followers_url":"https://api.github.com/users/nemothekid/followers","following_url":"https://api.github.com/users/nemothekid/following{/other_user}","gists_url":"https://api.github.com/users/nemothekid/gists{/gist_id}","starred_url":"https://api.github.com/users/nemothekid/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nemothekid/subscriptions","organizations_url":"https://api.github.com/users/nemothekid/orgs","repos_url":"https://api.github.com/users/nemothekid/repos","events_url":"https://api.github.com/users/nemothekid/events{/privacy}","received_events_url":"https://api.github.com/users/nemothekid/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-12-08T12:53:18Z","updated_at":"2015-12-09T15:18:30Z","closed_at":null,"body":"Currently, I have a default request object setup with the following options:\r\n\r\n```js\r\nvar options = {\r\n  baseUrl: window.location.protocol + baseUrl,\r\n  json: true,\r\n  simple: false,\r\n  resolveWithFullResponse: true,\r\n  auth: {\r\n    bearer: function() {\r\n      if (AdminStore == null) {\r\n        AdminStore = require(\"stores/admin\");\r\n      }\r\n      return AdminStore.getToken();\r\n    }\r\n  }\r\n};\r\n```\r\n\r\nWhen the user is not logged in, `getToken()` will return `null`. When this happens request auth code [will opt to send an empty Bearer token](https://github.com/request/request/blob/master/lib/auth.js#L45).\r\n\r\n`auth.js`\r\n```js\r\nif (sendImmediately || typeof sendImmediately === 'undefined') {\r\n    if (typeof bearer === 'function') {\r\n      bearer = bearer()\r\n    }\r\n    var authHeader = 'Bearer ' + (bearer || '')\r\n   self.sentAuth = true\r\n   return authHeader\r\n }\r\n```\r\n\r\nThis results in the Authorization header being set to `Authorization: Bearer `. For some reason Desktop Safari 9.0.1 and iOS Safari 9.1 don't like this, and when you try to set the header you get `DOM Exception 12: An invalid or illegal string was specified.` If I return a garbage string in `Bearer`, such as \"abc\", the error does not occur. (Possibly related: https://forum.ionicframework.com/t/solved-ios9-fails-with-setrequestheader-native-with-custom-headers-in-http-service/32399/8)\r\n\r\nIn any case, it seems to me the best option would be not set the header at all if the `bearer` function returns null."},"comment":{"url":"https://api.github.com/repos/request/request/issues/comments/163287103","html_url":"https://github.com/request/request/issues/1951#issuecomment-163287103","issue_url":"https://api.github.com/repos/request/request/issues/1951","id":163287103,"user":{"login":"simov","id":1694112,"avatar_url":"https://avatars.githubusercontent.com/u/1694112?v=3","gravatar_id":"","url":"https://api.github.com/users/simov","html_url":"https://github.com/simov","followers_url":"https://api.github.com/users/simov/followers","following_url":"https://api.github.com/users/simov/following{/other_user}","gists_url":"https://api.github.com/users/simov/gists{/gist_id}","starred_url":"https://api.github.com/users/simov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/simov/subscriptions","organizations_url":"https://api.github.com/users/simov/orgs","repos_url":"https://api.github.com/users/simov/repos","events_url":"https://api.github.com/users/simov/events{/privacy}","received_events_url":"https://api.github.com/users/simov/received_events","type":"User","site_admin":false},"created_at":"2015-12-09T15:18:30Z","updated_at":"2015-12-09T15:18:30Z","body":"Just make the `if (AdminStore == null)` outside of the `options` object and set the `auth` key only if needed."}}