What's new? | Help | Directory | Sign in
Google
opensocial-resources
Resources to help you spend less time coding, more time socializing
  
  
  
  
    
Search
for
Updated Jul 15, 2008 by apija...@google.com
GadgetsMakeRequest  
Examples of how to use the gadgets.io.makeRequest functions

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

Orkut users: This method currently has a bug. See http://code.google.com/p/opensocial-resources/issues/detail?id=2 for more information

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.coustenoble, Feb 08, 2008

Great examples and codes ! Thanks

Comment by nagarajuepuri, May 07, 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.ramamoorthy, Jul 01, 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.ramamoorthy, Jul 02, 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.


Sign in to add a comment