send

XHR2.send(method, url, options)

Makes synchronous HTTP requests to a web server and retrieve any type of data directly back into the script.

Parameters Summary

This method has the following parameters:

Parameters

Description

method

HTTP method name.

  • XHR2 supports the following methods:
    • DELETE
    • GET
    • POST
    • PUT
    • HEAD
    • OPTIONS
    • TRACE
  • XHR supports the following methods:
    • DELETE
    • GET
    • POST
    • PUT

url

String with resource URL.

options

JSON object with additional request parameters. The options are:

  • parameters – JSON object with query request parameters. They will be added to the URL.
  • headers – JSON object with request headers.
  • body – string or JSON object with request body. It can be byte array if isRawRequest is set to true.
  • isRawRequest – if request body is binary data (optional). Default value is false.
  • isRawResponse – if response body is binary data (optional). Default value is false.

Response

The method has the following general JSON response structure:

{
    "status" : <response_status>,
    "headers" : {
      [ <header1> : <value1>, ]
      [ <header2> : <value2>, ... ]
    },
    "body" : <response_body>
}

Response has the following parameters:

Paremeter

Description

status

HTTP response status code.

headers

JSON object with response headers.

body

String with response body. It can be JSON object if value of header Content-Type is application/json and response body is a valid JSON object. It can be byte array if isRawResponse is set to true.

Examples

GET Request

The following example sends the HTTP GET request to the Google Geocoding service.

var url = "http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&amp;sensor=false";
var response = XHR2.send("GET", url, {});
console.log(response);
{
    status: 200,
    headers: {
        'x-frame-options': 'SAMEORIGIN',
        'content-type': 'application/json; charset=UTF-8',
        'cache-control': 'public, max-age=86400',
        'x-xss-protection': '1; mode=block',
        expires: 'Thu, 14 Jul 2016 20:52:11 GMT',
        'access-control-allow-origin': '*',
        'content-length': 527,
        server: 'mafe',
        'content-encoding': 'gzip',
        date: 'Wed, 13 Jul 2016 20:52:11 GMT',
        vary: 'Accept-Language'
    },
    body: '{\n "results" : [\n {\n "address_components" : [\n {\n "long_name" : "1600",\n "short_name" : "1600",\n "types" : [ "street_number" ]\n },\n {\n "long_name" : "Amphitheatre Parkway",\n "short_name" : "Amphitheatre Pkwy",\n "types" : [ "route" ]\n },\n {\n "long_name" : "Mountain View",\n "short_name" : "Mountain View",\n "types" : [ "locality", "political" ]\n },\n {\n "long_name" : "Santa Clara County",\n "short_name" : "Santa Clara County",\n "types" : [ "administrative_area_level_2", "political" ]\n },\n {\n "long_name" : "California",\n "short_name" : "CA",\n "types" : [ "administrative_area_level_1", "political" ]\n },\n {\n "long_name" : "United States",\n "short_name" : "US",\n "types" : [ "country", "political" ]\n },\n {\n "long_name" : "94043",\n "short_name" : "94043",\n "types" : [ "postal_code" ]\n }\n ],\n "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",\n "geometry" : {\n "location" : {\n "lat" : 37.422364,\n "lng" : -122.084364\n },\n "location_type" : "ROOFTOP",\n "viewport" : {\n "northeast" : {\n "lat" : 37.4237129802915,\n "lng" : -122.0830150197085\n },\n "southwest" : {\n "lat" : 37.42101501970851,\n "lng" : -122.0857129802915\n }\n }\n },\n "place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",\n "types" : [ "street_address" ]\n }\n ],\n "status" : "OK"\n}\n'
}

GET Request

This example is the same as the first one but now only printing body as JSON.

var url = "http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&amp;sensor=false";
var response = XHR2.send("GET", url, {});
console.log(response.body);
{
    "results": [{
        "address_components": [{
            "long_name": "1600",
            "short_name": "1600",
            "types": ["street_number"]
        }, {
            "long_name": "Amphitheatre Parkway",
            "short_name": "Amphitheatre Pkwy",
            "types": ["route"]
        }, {
            "long_name": "Mountain View",
            "short_name": "Mountain View",
            "types": ["locality", "political"]
        }, {
            "long_name": "Santa Clara County",
            "short_name": "Santa Clara County",
            "types": ["administrative_area_level_2", "political"]
        }, {
            "long_name": "California",
            "short_name": "CA",
            "types": ["administrative_area_level_1", "political"]
        }, {
            "long_name": "United States",
            "short_name": "US",
            "types": ["country", "political"]
        }, {
            "long_name": "94043",
            "short_name": "94043",
            "types": ["postal_code"]
        }],
        "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
        "geometry": {
            "location": {
                "lat": 37.422364,
                "lng": -122.084364
            },
            "location_type": "ROOFTOP",
            "viewport": {
                "northeast": {
                    "lat": 37.4237129802915,
                    "lng": -122.0830150197085
                },
                "southwest": {
                    "lat": 37.42101501970851,
                    "lng": -122.0857129802915
                }
            }
        },
        "place_id": "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
        "types": ["street_address"]
    }],
    "status": "OK"
}
Request with Basic Authentication

This example shows invoking an API that uses Basic Authentication. Basic Authentication usually has this form: http://[email protected]@host.com. To invoke this from a script add the Authorization header. The [email protected] encoding can be done using browser's console by running this code: window.btoa("[email protected]");.

var url = "https://host.com/service"; // REST API URL
var XHRResponse = XHR2.send("GET", url, {
   "headers": {
      "Authorization": "Basic <encoded_string>"
   }
});
Apperyio.response.success(XHRResponse.body, "application/json");
POST Request

The following examples sends an HTTP POST request to Mailgun API to send an email.

// Mailgun domain in the form: sandbox123abc.mailgun.org
var domain = "sandbox123abc.mailgun.org";

// Mailgun endpiont to send an email message
var url = "https://api.mailgun.net/v3/"+domain+"/messages";

var from = "Excited User <[email protected]>";
var to   = request.get("to");
var subject = request.get("subject");
var text    = request.get("text");

var XHRResponse = XHR2.send("POST", url, {
  "parameters": {
    "from" : from,
    "to" : to,
    "subject": subject,
    "text": text
  },
  "headers": {
    "Authorization": "Basic YXBpOmtleS0wY2UxNDM5ZGJiNjczOQ==") // For API key, see Mailgun domains page: https://mailgun.com/app/domains
  } 
});

Apperyio.response.success(XHRResponse.body, "application/json");
{
    "message": "Queued. Thank you.",
    "id": "<[email protected]>"
}
Invoking an API Express Service

This examples show invoking an API Express service that dost a POST and its body is set to JSON such as {"param1": num}.

var url = "https://api.appery.io/rest/1/apiexpress/api/test/me?apiKey=.....";
var XHRResponse = XHR2.send("POST", url, {
   "headers": {
      "Content-Type": "application/json" 
   },
   "body": {"param1" : 45}
});
Apperyio.response.success(XHRResponse.body, "application/json");