Personal tools

API Request Formats

From SwitchvoxExtendAPI

Revision as of 18:43, 7 December 2011 by Dwp (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

All the examples in the documentation wiki are for using the XML format with the API, but there are multiple formats that can be requested from the Extend API. Currently the only other format is JSON and below are examples on how to use the JSON interface to pass a JSON request and receive back a JSON response.


Contents

API Formats

XML

All the examples in the API documentation use the XML format. If you need additional information about this format please see the Constructing Requests section.


JSON

JSON stands for JavaScript Object Notation and is the preferred format for passing data to JavaScript programs. To read more about JSON please see the Wikipedia JSON Article or for an introduction to json visit json.org.


Connecting to the JSON Interface

If you haven't already read the Connecting to the Core Interface, please visit that page now. That page goes over how to connect to the XML interface, and connecting to the JSON interface is very similar. The URL to connect to the JSON interface is below:

	https://YOUR.PBX.IP/json


Constructing JSON Requests

If you haven't already read the Constructing Requests, please visit that page now. Now that you know how to construct XML requests, the next step is to understand the simple rules on how to convert XML into JSON. Below are some examples that illustrate how to convert various XML tags and formats into their equivalent JSON form.


Converting A Request

This XML:

<request method="switchvox.extensions.getInfo">
    <parameters>
        <extensions>
            <extension>100</extension>
            <extension>101</extension>
        </extensions>
    </parameters>
</request>

Turns into this JSON:

{
	"request": {
		"method": "switchvox.extensions.getInfo", 
		"parameters": {
			"extensions": [ "100",  "101" ]
		}
	}
}


Converting A Response : Example 1

This XML:

<response method="switchvox.extensions.getInfo">
	<result>
		<extensions>
				<extension number="105" account_id="1138" display="David Podolsky" />
		</extensions>
	</result>
</response>

Becomes this JSON:

{
  "response" : {
    "method" : "switchvox.extensions.getInfo",
    "result" : {
      "extensions" : {
        "extension" : {
          "number" : 105,
          "account_id" : 1138,
          "display" : "David Podolsky",
        }
      }
    }
  }
}

Converting A Response : Example 2 (array)

In this example the response has multiple extensions returned. If there is a tag with the same name then this is converted into an array.

<response method="switchvox.extensions.getInfo">
	<result>
		<extensions>
				<extension number="101" account_id="1001" display="Bob White" />
				<extension number="103" account_id="1002" display="Tina Green" />
		</extensions>
	</result>
</response>

Becomes this JSON:

{
   "response" : {
      "method" : "switchvox.extensions.getInfo",
      "result" : {
         "extensions" : {
            "extension" : [
               {
                  "number" : "101",
                  "account_id" : "1002",		
                  "display" : "Bob White"
               },
               {
                  "number" : "103",
                  "account_id" : "1002"
                  "display" : "Tina Green",
               }
            ]
         }
      }
   }
}


Just Use the Test Suite

An easy way to look at the sample Request and Response formats of JSON is to use the [Core Test Suite]. You can type in your request and have it spit out in any format you want.