My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
GadgetsMakeRequest  
Examples of how to use the gadgets.io.makeRequest functions
Updated Feb 4, 2010 by gooseman...@gmail.com

Introduction

In 0.7, opensocial.makeRequest was changed to gadgets.io.makeRequest. The following samples show how to make various queries with the new syntax.

Samples on this page use the output function syntax supplied by the CodeRunner gadget

Details

Caching

Calls to makeRequest are cached. This wrapper function takes the same parameters as the makeRequest call, but accepts another parameter named refreshInterval which allows you to specify the cache duration. Specifying 0 will mean no caching.

function makeCachedRequest(url, callback, params, refreshInterval) {
  var ts = new Date().getTime();
  var sep = "?";
  if (refreshInterval && refreshInterval > 0) {
    ts = Math.floor(ts / (refreshInterval * 1000));
  }
  if (url.indexOf("?") > -1) {
    sep = "&";
  }
  url = [ url, sep, "nocache=", ts ].join("");
  gadgets.io.makeRequest(url, callback, params);
}

You can call this function in places where you would use gadgets.io.makeRequest(), just put the extra parameter at the end of the method arguments.

Responses

Responses come back as the following JavaScript object:

{
  data : <parsed data, if applicable>,
  errors : <any errors that occurred>,
  text : <raw text of the response>
}

Normal Requests

function makeNormalRequest() {  
  var params = {};
  params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;
  var url = "http://graargh.returnstrue.com/buh/makerequest.php";
  gadgets.io.makeRequest(url, response, params);
};

function response(obj) {
  //obj.text contains the text of the page that was requested
  output(obj.text);
};

makeNormalRequest();

DOM Requests

function makeDOMRequest() {
  var params = {};
  params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
  var url = "http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml";
  gadgets.io.makeRequest(url, response, params);
};

function response(obj) {
  //obj.data contains a Document DOM element corresponding to the page that was requested
  output(obj.data);
};

makeDOMRequest();

JSON Requests

function makeJSONRequest() {
  var params = {};
  params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
  var url = "http://graargh.returnstrue.com/buh/fetchme.php";
  gadgets.io.makeRequest(url, response, params);
};

function response(obj) {
  //obj.data contains a JavaScript object corresponding to the data that was requested
  output(obj.data);
};

makeJSONRequest();

Feed Requests

Currently does not work on Orkut

function makeFeedRequest() {
  var params = {};
  params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.FEED;
  var url = "http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml";
  gadgets.io.makeRequest(url, response, params);
};

function response(obj) {
  //obj.data contains a JavaScript object corresponding to the feed that was requested
  output(obj.data);
};

makeFeedRequest();

POST Requests

function makePOSTRequest() {
  var params = {};
  var postdata = {
    data1 : "test",
    data2 : 1234566
  };
  params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
  params[gadgets.io.RequestParameters.POST_DATA] = gadgets.io.encodeValues(postdata);
  var url = "http://graargh.returnstrue.com/buh/makerequest.php";
  gadgets.io.makeRequest(url, response, params);
};

function response(obj) {
  output(obj.text);
};

makePOSTRequest();

Signed Requests

function makeSignedRequest() {
  var params = {};
  params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
  var url = "http://graargh.returnstrue.com/buh/fetchme.php";
  gadgets.io.makeRequest(url, response, params);
};

function response(obj) {
  //obj.text contains the text of the response
  output(obj.text);
};

makeSignedRequest();
Comment by victor.c...@gmail.com, Feb 8, 2008

Great examples and codes ! Thanks

Comment by nagaraju...@gmail.com, May 7, 2008

The above example under feed request is returning "null" value... is this something wrong with the code or else its the problem from Gadget API. I have tried the above code with all possible feeds, even though its returning same "null" value for all the feed URLs

Comment by muthiah....@gmail.com, Jul 1, 2008

makeDOMRequest() works very well. Is there any code snippet that shows how to parse the XML using javascript to display all titles?

Comment by muthiah....@gmail.com, Jul 2, 2008

I see the answer at http://code.google.com/apis/opensocial/articles/makerequest.html; I see conflicting statements about content-type. Some articles recommend using content-type="url" for commercial gadgets. http://code.google.com/apis/gadgets/docs/publish.html#PageCreator recommends to use content-type="html" to optimize for heavy traffic.

Comment by sushant...@gmail.com, Jan 28, 2009

When using makeRequest the response cotain the result and the following error message.

When using makeRequest the response contain the correct result and an error message.
throw 1; < don't be evil' >{..... "rc":200}}
Please give the example for return response object to return response object without this error. Please give the example of response object.
Comment by apijason...@gtempaccount.com, Jan 28, 2009

That isn't an error message, and you should be able to ignore it.

If you're fetching plain text/HTML, you can use the text property of the object passed into the callback:

function callback(resp) {

// output resp.text
}

If you're fetching JSON or XML from your server, use the data property instead:

function callback(resp) {

// use resp.data
}

Comment by jhonatar...@yahoo.com.br, Feb 2, 2009

uguyh

Comment by gerrys...@gmail.com, Feb 5, 2009

That's great, but why does no one, and I mean no one, ever, ever, ever talk about the server side? I'm getting a request as a GET even though I set it to POST and I can't access the post variables (like test/123456 above) either. Help!

Comment by apijason...@gtempaccount.com, Feb 6, 2009

If you are having any problems using makeRequest with a particular container (e.g. orkut, iGoogle, etc.), please post in that container's discussion group. POST requests should definitely be supported by all networks.

Comment by alots....@gmail.com, Jun 2, 2009

i tried to fetch a plain html for server but, i think that orkut environment does not accept. Does anyone know about that??

Comment by brunohpl...@gmail.com, Feb 2, 2010

valeu! mas eu queria saber mais sobre as diferencas entre cada um

Comment by ovw.t...@gmail.com, Mar 12, 2010

I liked the idea very much and implemented it on my gadget. However, note that according to the makeRequest documentation '...Signed requests and objects with POST_DATA present will generally not be cached. ' . As I´m using OAuth and many posts, for instance, this didn´t take effect, unfortunatelly.I even had lots of 502(timeout) errors from the proxy... But thank you anyway for sharing it with us !

Comment by ovw.t...@gmail.com, Mar 12, 2010

Just correcting... error 504(timeout)...

Comment by ryandewh...@gmail.com, Oct 18, 2010

I'm getting this error: 'output not defined' in the Firefox error console.

Any ideas/help would be much appreciated. I've been over and over the examples on this page and can't find a solution.

Thank you, Ryan

Comment by justin.kruger, Oct 18, 2010

might the response type be wrong? check your params.

Comment by ryandewh...@gmail.com, Oct 19, 2010

Not sure I know what you mean by the 'response type'. I have tried the following JS objects, data, error and text. They all come back with the same error. The code is being called as it is viewable when I view source in GMail but this maybe down to the Preload tag.

Here is my code: http://pastie.org/private/wodhxhq3ekgjc5yo9aybw

Any help is much appreciated.

Thank you, Ryan

Comment by mailtopr...@gmail.com, Sep 21, 2011

xxxxxxxx

Comment by mailtopr...@gmail.com, Sep 21, 2011

ccccccccccccccccccccc


Sign in to add a comment
Powered by Google Project Hosting