Skip to content

Commit

Permalink
If Meteor.http.call JSON-ifies the request body, default the Content-…
Browse files Browse the repository at this point in the history
…Type to

application/json.

This matches our heuristic for deciding whether to parse responses as JSON.
  • Loading branch information
glasser committed Dec 28, 2012
1 parent 297f20d commit 3bada2a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
13 changes: 9 additions & 4 deletions packages/http/httpcall_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ Meteor.http = Meteor.http || {};

method = (method || "").toUpperCase();

var headers = {};

var content = options.content;
if (options.data)
if (options.data) {
content = JSON.stringify(options.data);
headers['Content-Type'] = 'application/json';
}

var params_for_url, params_for_body;
if (content || method === "GET" || method === "HEAD")
Expand Down Expand Up @@ -51,6 +55,8 @@ Meteor.http = Meteor.http || {};
content = Meteor.http._encodeParams(params_for_body);
}

_.extend(headers, options.headers || {});

////////// Callback wrapping //////////

// wrap callback to always return a result object, and always
Expand Down Expand Up @@ -84,9 +90,8 @@ Meteor.http = Meteor.http || {};

xhr.open(method, url, true, username, password);

if (options.headers)
for (var k in options.headers)
xhr.setRequestHeader(k, options.headers[k]);
for (var k in headers)
xhr.setRequestHeader(k, headers[k]);


// setup timeout
Expand Down
9 changes: 5 additions & 4 deletions packages/http/httpcall_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ Meteor.http = Meteor.http || {};

var url_parts = url_util.parse(url);

var headers = {};

var content = options.content;
if (options.data)
if (options.data) {
content = JSON.stringify(options.data);
headers['Content-Type'] = 'application/json';
}


var params_for_url, params_for_body;
Expand All @@ -42,9 +46,6 @@ Meteor.http = Meteor.http || {};
url_parts.protocol+"//"+url_parts.host+url_parts.pathname,
url_parts.search, options.query, params_for_url);


var headers = {};

if (options.auth) {
if (options.auth.indexOf(':') < 0)
throw new Error('auth option should be of the form "username:password"');
Expand Down
14 changes: 14 additions & 0 deletions packages/http/httpcall_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,20 @@ testAsyncMulti("httpcall - methods", [
test.equal(result.statusCode, 200);
var data = result.data;
test.equal(data.body, {greeting: "Hello World!"});
test.equal(data.headers['content-type'], 'application/json');
}));

Meteor.http.call(
"POST", url_prefix()+"/data-test-explicit",
{ data: {greeting: "Hello World!"},
headers: {'Content-Type': 'text/stupid'} },
expect(function(error, result) {
test.isFalse(error);
test.isTrue(result);
test.equal(result.statusCode, 200);
var data = result.data;
test.equal(data.body, {greeting: "Hello World!"});
test.equal(data.headers['content-type'], 'text/stupid');
}));
}
]);
Expand Down

0 comments on commit 3bada2a

Please sign in to comment.