{"action":"created","issue":{"url":"https://api.github.com/repos/meteor/meteor/issues/4419","labels_url":"https://api.github.com/repos/meteor/meteor/issues/4419/labels{/name}","comments_url":"https://api.github.com/repos/meteor/meteor/issues/4419/comments","events_url":"https://api.github.com/repos/meteor/meteor/issues/4419/events","html_url":"https://github.com/meteor/meteor/pull/4419","id":78711799,"number":4419,"title":"Added beforeSend hook  to http client","user":{"login":"robertpitt","id":567579,"avatar_url":"https://avatars.githubusercontent.com/u/567579?v=3","gravatar_id":"","url":"https://api.github.com/users/robertpitt","html_url":"https://github.com/robertpitt","followers_url":"https://api.github.com/users/robertpitt/followers","following_url":"https://api.github.com/users/robertpitt/following{/other_user}","gists_url":"https://api.github.com/users/robertpitt/gists{/gist_id}","starred_url":"https://api.github.com/users/robertpitt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/robertpitt/subscriptions","organizations_url":"https://api.github.com/users/robertpitt/orgs","repos_url":"https://api.github.com/users/robertpitt/repos","events_url":"https://api.github.com/users/robertpitt/events{/privacy}","received_events_url":"https://api.github.com/users/robertpitt/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/meteor/meteor/labels/Project:Utilities:HTTP","name":"Project:Utilities:HTTP","color":"bfd4f2"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-05-20T21:50:03Z","updated_at":"2015-06-17T20:44:59Z","closed_at":"2015-06-16T20:46:08Z","pull_request":{"url":"https://api.github.com/repos/meteor/meteor/pulls/4419","html_url":"https://github.com/meteor/meteor/pull/4419","diff_url":"https://github.com/meteor/meteor/pull/4419.diff","patch_url":"https://github.com/meteor/meteor/pull/4419.patch"},"body":"This small PR is to help solve the issue caused by the xhr object being locked in the `http.call` closure.\r\n\r\nThis PR add's a small callback that's fired just before the `xhr.send` is called, allowing for the xhr object to be manipulated to allow developers to take full control over request.\r\n\r\nIncluding but not limited to\r\n\r\n- Monitoring the state of the request.\r\n- Monitoring the progress of the request.\r\n- Changing the behaviour of the request (e.g. `async: false`).\r\n- Performing XHR based async file uploads.\r\n- Aborting the request (e.g. within a `'progress'` event..)\r\n\r\nReason for implementing the hook in this fasion, i.e. named `beforeSend` and the `xhr` + `options` being passed over is to replicate the implementation that `jQuery` provides:\r\n\r\n> **beforeSend**\r\n> Type: **Function**( **jqXHR** jqXHR, **PlainObject** settings )\r\n> A pre-request callback function that can be used to modify the jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object before it is sent. Use this to set custom headers, etc. The jqXHR and settings objects are passed as arguments. This is an Ajax Event. Returning false in the beforeSend function will cancel the request. As of jQuery 1.5, the beforeSend option will be called regardless of the type of request.\r\n\r\nSource: http://api.jquery.com/jquery.ajax/\r\n\r\nThanks"},"comment":{"url":"https://api.github.com/repos/meteor/meteor/issues/comments/112945237","html_url":"https://github.com/meteor/meteor/pull/4419#issuecomment-112945237","issue_url":"https://api.github.com/repos/meteor/meteor/issues/4419","id":112945237,"user":{"login":"smeijer","id":1196524,"avatar_url":"https://avatars.githubusercontent.com/u/1196524?v=3","gravatar_id":"","url":"https://api.github.com/users/smeijer","html_url":"https://github.com/smeijer","followers_url":"https://api.github.com/users/smeijer/followers","following_url":"https://api.github.com/users/smeijer/following{/other_user}","gists_url":"https://api.github.com/users/smeijer/gists{/gist_id}","starred_url":"https://api.github.com/users/smeijer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smeijer/subscriptions","organizations_url":"https://api.github.com/users/smeijer/orgs","repos_url":"https://api.github.com/users/smeijer/repos","events_url":"https://api.github.com/users/smeijer/events{/privacy}","received_events_url":"https://api.github.com/users/smeijer/received_events","type":"User","site_admin":false},"created_at":"2015-06-17T20:44:59Z","updated_at":"2015-06-17T20:44:59Z","body":"@glasser, I've noticed you have quoted me. I completely agree with you that one should use an external xhr library if the project makes a lot of use of xhr requests (I'm a big fan of [visionmedia/superagent](https://github.com/visionmedia/superagent)). In my case, I required only a single xhr request in my entire app. For a file upload template only. \r\n\r\nSo my thought at the time was, if the xhr object could be accessed, to be able to bind some events like `progress` to it, than it wouldn't be necessary to load yet another library and thereby add another dependency to the project. So my thinking was a lot like what you're writing. I wished to use `Meteor.http`, simply because it was already there. And I find it (most often) wrong to add multiple libraries to a single project, that share mostly the same purpose. (like two xhr libs, with only a different syntax).\r\n\r\nI also understand, and agree with you, that `Meteor.http` will never get as good as a library that is 100% focused on handling http requests. But I also don't believe that it is expected to. Actually, by providing a way to access the `xhr` object - by implementing the `beforeSend` as @robertpitt has done - it is now possible to make wrapper packages.\r\n\r\nSo, as its now possible to create light weight http packages that build on top of `Meteor.http`, I don't expect much more \"*feature requests to make it marginally more useful*\", but I expect wrapper packages instead.\r\n\r\nAnyway, thanks @robertpitt for the PR, and @glasser for the merge :+1: "}}