It already took me several hours implementing cookie on url MeteorJS. What I need to do is that, pass a cookie data n url like 'CURLOPT_COOKIE' PHP. I cant find any example code on their docs and even to forums. For now I have these functions:
/* HTTP REQUEST */
Meteor.methods({
httpRequest: function(type, uri, params){
this.unblock();
check(type, String);
check(uri, String);
check(params, Object);
try {
var result = HTTP.call(type, uri, {params: params});
return result;
} catch (e) {
// Got a network error, time-out or HTTP error in the 400 or 500 range.
return e;
}
}
});
// HTTP request with cooki
getUserDetails: function(session_id, uid){
var params = {
headers: {
Cookie: {
sessid: session_i
}
},
uid: uid
};
var response = Meteor.call('httpRequest', "POST", "http://example.com/rest/wp /alt_wp_resources/loaduser.json", params);
//res = JSON.parse(response.content);
return response;
}
// call here
Meteor.startup(function () {
// delay for 5 sec
Meteor.setTimeout(function (){
Meteor.call('getUserCredentials', 'api12345', '123qweasd', function (error, result) {
// check user authentication
var success = result.success;
console.log(result);
// user has account from lpgp site, let's save him to meteor.
if (success){
console.log('success');
var session_id = result.session_id;
//console.log(_session_id);
Meteor.call('getUserDetails', 'SESSba071091c09f79fefd66e4884dcdde50', 68558, function (error, result) {
if (!error)
console.log(result);
else
console.log(error);
});
}else
// app can't find user account from lpgp site.
console.log(error);
});
}, 5000);
});
The call is successful but, just returned a success: false.
Response:
Object {statusCode: 200, content: "{"success":false}", headers: Object, data: Object}
Meteor's HTTP module on the server side is merely a wrapper for the npm module named
request. Therequestnpm module includes support for specifying your own cookies as well as saving them into a cookie jar (just follow the link and search for 'cookie'). The default cookie jar istough-cookieand interestingly, Meteor includes it even though I don't see any way to use it from Meteor.HTTP.The upshot of these implementation details is that you can use
requestdirectly. I took a similar approach to wrapping request as Meteor's HTTP module but instead of the restricted sub-set of options that HTTP provides, my wrapper allows full access to all the capability ofrequestandtough-cookie. The cool part is that you don't even need to directly addrequestas a dependency on your own since it's already a dependency of Meteor. The risk, of course, is that a later version of Meteor could use something besidesrequestand your code would break.Anyway, here is my own wrapper for
request. It includes an example of JSessionID cookie support for making Jenkins API calls. Just put this into a filesyncRequest.coffeeunder the\serverfolder and make sure you have added thecoffeescriptpackage (Meteor add coffeescript)... or compile my code and save it to a .js file in the\serverfolder.