My favorites | Sign in
Google
                
Search
for
Updated Sep 11, 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

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.

Comment by sushanthcs, 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 apija...@google.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 02, 2009

uguyh

Comment by gerryscat, Feb 05, 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 apija...@google.com, Feb 06, 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.ssa, Jun 02, 2009

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


Sign in to add a comment