UserGuide

Eddie's Electronics Sample Web Service

From Xojo Documentation

In order to help you with testing a web service, the Eddie's Electronics web service is available for use with your apps. This web service is hosted on Xojo Cloud as a Xojo web app and uses a SQLite database for its data.

Base URL

All web services calls are done through the base URL:

   http://demos.xojo.com/EEWS/index.cgi/api/


GetAllCustomers

HTTP Method: GET or POST

This API call returns a JSON document containing the object "GetAllCustomers" which is a key-value store of a customer ID and its data, including FirstName, LastName, City, State and Zip.

Sample Code

This code sends the web service request using an HTTPSocket:

// Add Xojo.Net.HTTPSocket subclass to your project.
// Drag it to a layout and name it MySocket.
MySocket.Send("POST", "http://demos.xojo.com/EEWS/index.cgi/api/GetAllCustomers%22)

This code (in the PageReceived event handler of the socket added to your layout) processes the resulting Dictionary:

Dim jsonData As Text = Xojo.Core.TextEncoding.UTF8.ConvertDataToText(Content)

Dim json As Xojo.Core.Dictionary
json = Xojo.Data.ParseJSON(jsonData)

Dim customers As Xojo.Core.Dictionary
customers = json.Value("GetAllCustomers")

For Each entry As Xojo.Core.DictionaryEntry In customers
Dim custDict As Xojo.Core.Dictionary = entry.Value
Dim firstName As Text = custDict.Value("FirstName")
Next

Sample JSON Result

{
"GetAllCustomers":
    {
        "10174":
        {
            "FirstName":"Abdul",
            "LastName":"Mcconnell",
            "City":"Colorado Springs",
            "State":"CO",
            "Zip":"80935"
        },
        "10179":
        {
            "FirstName":"Abel",
            "LastName":"Alexander",
            "City":"Arcadia",
            "State":"IA",
            "Zip":"51430"
        }
    }
}

GetCustomer

HTTP Method: POST

This API call returns all the details for a single customer. You specify the customer by providing a simple JSON document (as request content) containing the ID of the customer you want.

Sample Xojo Code

This code (using the same socket used in the example above) sends a request for customer data:

Dim cust As New Xojo.Core.Dictionary
cust.Value("ID") = 10179

Dim json As Text
json = Xojo.Data.GenerateJSON(cust)

Dim data As Xojo.Core.MemoryBlock
data = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(json)

MySocket.SetRequestContent(data, "application/x-www-form-urlencoded")
MySocket.Send("POST", "http://demos.xojo.com/EEWS/index.cgi/api/GetCustomer%22)

This code parses the resulting data (in the PageReceived event handler for the HTTPSocket):

Dim jsonData As Text = Xojo.Core.TextEncoding.UTF8.ConvertDataToText(Content)

Dim json As Xojo.Core.Dictionary
json = Xojo.Data.ParseJSON(jsonData)

Dim custInfo As Xojo.Core.Dictionary
custInfo = json.Value("GetCustomer")

Dim id As Integer
Dim firstName, lastName As Text
For Each entry As Xojo.Core.DictionaryEntry In custInfo
Select Case entry.Key
Case "ID"
id = Integer.FromText(entry.Value)
Case "FirstName"
firstName = entry.Value
Case "LastName"
lastName = entry.Value
End Select
Next

Sample JSON Request

This JSON asks for information for customer ID 10179:

{"ID": 10179}

Sample JSON Result

This is the resulting JSON containing information for customer 10179:

{
    "GetCustomer":
    {
        "ID":"10179",
        "FirstName":"Abel",
        "LastName":"Alexander",
        "City":"Arcadia",
        "State":"IA",
        "Zip":"51430",
        "Phone":"1-261-529-7025",
        "Email":"elit.sed@aliquamarcu.edu",
        "Photo":"Base64EncodedData",
        "Taxable":"0"
    }
}