This page last changed on Jan 26, 2012 by jed.wheeler@involver.com.

Request

Visits and returns the content of any endpoint via get, post, put, or delete.  You can use this tool to push data to a remote restful api or pull data in from one.

Available Request Types

  • GET: sml.web.Request.get(url,options)
  • POST: sml.web.Request.post(url,options)
  • PUT: sml.web.Request.put(url,options)
  • DELETE: sml.web.Request._delete(url,options) Note: underscore, `_delete`.

Params

name type description
url string The url to visit.
options object The place to pass additional params, and callbacks
options.headers object or string Any headers that are needed for the request.
options.params object or string Any additional query params not found in the url.
options.body object or string The body of the request, place any parameters that can't be sent in the url here.
options.success function The single argument callback upon successful completion of the request.
options.error function The single argument callback upon a failure.

Return values

name type description
data object Upon success this object will be populated with information about the request and response.
data.status integer A hint about how the external request completed. {200,204,400,500} see Status codes.
data.message string An informative message about the response.
data.request_method string The type of request that was received by the server.
data.requested_url string The original url.
data.redirected_url string The last url. aka. The url that data.response refers to
data.headers string The headers of the remote request simply forwarded here.
data.response any The format of this field is subject to the response of the remote server.
Example success response
    {
        status : 200,
        message : 'ok',
        request_method : 'get',
        requested_url : 'http://bit.ly/sf_trends',
        redirected_url : 'http://api.twitter.com:80/trends.json',
        headers : 'X-RateLimit-Remaining=128',
        response : {
            "trends":[{...}, {...}],
            "locations":[{"name":"San Francisco","woeid":2487956}],
            "as_of":"2011-11-21T20:29:10Z"}
        }
    }
Example error response
    {
        status : 400,
        message : 'Required parameters missing. Hint: no url provided',
	response : {}
    }

Status codes

code meaning
200 request finished ok.
204 empty response.
400 invalid parameters.
500 the server encountered an error in processing your request.

Examples

Fetch the Twitter trends for San Francisco:

<script type="text/javascript">
var remote_url = "http://api.twitter.com/1/trends/2487956.json"
var fetch_timeline = function() {
    sml.web.Request.get(remote_url,{
        success: function(data) {
            console.log(data);
        },
        error: function() {
            alert("request failed");
        }
    });
}
</script>

<a href="#_" onclick="fetch_timeline();">Fetch Twitter Trends for San Francisco</a>

Post signup data to your server:

<script type="text/javascript">
...
// somewhere in the onSubmit handler
...
    var remote_url = "http://api.your_restful_website.com/signup/create";
    var body_args = {
        email : $.('#email_field').val(),
        name : $.('#full_name_field').val(),
        other_field : $.('#some_other_field').val()
    };

    sml.web.Request.post(remote_url,{
        params : { app_token : '4eFg40....3kfksd' },
        body : body_args,
        success: function(data) {
            $.('#some_spinner).hide();
            $.('#successfully_posted_data).show();
        },
        error: function() {
            $.('#some_spinner).hide();
            $.('#did_not_successfully_post_data).show();
        }
    });
...
</script>
Document generated by Confluence on Feb 12, 2013 09:09