How to make an API call using meteor
Solution 1
You are defining your checkTwitter Meteor.method inside a client-scoped block. Because you cannot call cross domain from the client (unless using jsonp), you have to put this block in a Meteor.isServer
block.
As an aside, per the documentation, the client side Meteor.method
of your checkTwitter function is merely a stub of a server-side method. You'll want to check out the docs for a full explanation of how server-side and client-side Meteor.methods
work together.
Here is a working example of the http call:
if (Meteor.isServer) {
Meteor.methods({
checkTwitter: function () {
this.unblock();
return Meteor.http.call("GET", "http://search.twitter.com/search.json?q=perkytweets");
}
});
}
//invoke the server method
if (Meteor.isClient) {
Meteor.call("checkTwitter", function(error, results) {
console.log(results.content); //results.data should be a JSON object
});
}
Solution 2
This might seem rudimentary - but the HTTP package does not come by default in your Meteor project and requires that you install it a la carte.
On the command line either:
Just Meteor:
meteor add httpMeteorite:
mrt add http
Solution 3
Meteor.http.get on the client is async, so you will need to provide a callback function :
Meteor.http.call("GET",url,function(error,result){
console.log(result.statusCode);
});
Solution 4
Use Meteor.http.get
. Per the docs:
Meteor.http.get(url, [options], [asyncCallback]) Anywhere Send an HTTP GET request. Equivalent to Meteor.http.call("GET", ...).
The docs actually include some examples of using Twitter, so you should be able to get started with them.
Related videos on Youtube
Comments
-
iJade almost 2 years
Ok here is the twitter API,
http://search.twitter.com/search.atom?q=perkytweets
Can any one give me any hint about how to go about calling this API or link using Meteor
Update::
Here is the code that i tried but its not showing any response
if (Meteor.isClient) { Template.hello.greeting = function () { return "Welcome to HelloWorld"; }; Template.hello.events({ 'click input' : function () { checkTwitter(); } }); Meteor.methods({checkTwitter: function () { this.unblock(); var result = Meteor.http.call("GET", "http://search.twitter.com/search.atom?q=perkytweets"); alert(result.statusCode); }}); } if (Meteor.isServer) { Meteor.startup(function () { }); }
-
Opentuned over 11 yearsI had a quick look at the docs and i reckon it will be useful to look at docs.meteor.com/#meteor_http_get , although i havent used meteor before so im not too sure the syntax, but ill have a look now for you...
-
Jon Onstott almost 9 yearsThe documentation for
HTTP.get()
is now at docs.meteor.com/#/full/http_get
-
-
iJade over 11 yearsthnks for dat..i just updated my code but its still not working.Showing no output
-
iJade over 11 yearswell it actually returns xml and not json,so while trying to do an alert it shows undefined
-
TimDog over 11 yearssearch.twitter.com/search.json?q=perkytweets (edited above) -- and more importantly, did this work?
-
iJade over 11 yearswell actually i tried with ur updated answer too,i'm getting a response but it is [object][object].Is it possible to convert it into string.I'm mean in normal javascript we can use jquery JSON.stringify to convert json to string but here if i try dat i'm getting {"error":{}}
-
TimDog over 11 yearsI just ran the code and it looks good.
results.content
should give you a string. I'm editing my answer though, as option 1 isn't going to fly -- you cannot do cross domain ajax client side (unless leveraging jsonp, but I don't want to complicate things). -
kontur over 9 yearsAlso note for anyone reading this in retrospect: You need to make the response of this async call available as a reactive variable, for example by storing it into a Session variable, which you then print in the template (and autoupdate when the async call completes).
-
Cees Timmerman almost 9 yearsSilently fails without the package, so
meteor add http
. -
Rahul Khatri over 8 yearsHi, where are you using the API keys? Is it running without API keys?