The XURL package has replaced the REST package from Xill IDE version 3.1 onwards.

The XURL package is used to send HTTP requests and get a response from the server. The following requests are available:

  • Delete: to delete a resource;
  • Get: to retrieve resources;
  • Head: to retrieve the header of a resource;
  • Options: to retrieve a list of all possible HTTP requests on a server;
  • Patch: to partially update a resource;
  • Post: to create a new resource;
  • Put: to partially or fully update a resource.

Examples

One of the most used functions is the Get function. It is used for obtaining a resource from a server. In the example below, we retrieve the information about three books from the Google Books API and print the title, author and page count. Before printing the information, there is a check on the status code, to see if the request was succesful.

 

 

use XURL,System,String;

var baseLink    = "https://www.googleapis.com/books/v1/volumes?q=isbn:"; // Link to server
var ISBNs       = ["0262032937","0201633612","0321356683"];              // List of books 

// Prints information about all books
foreach(isbn in ISBNs){
    var bookInformation = getBookInformation(isbn);
    if(bookInformation){            
        printBookInformation(getBookInformation(isbn));
    }
}

// Obtains information about a given book from the server using a Get request
function getBookInformation(isbn){
    var response = XURL.get(baseLink :: isbn);                  // Do Get request
    if(response.status.code == 200){                            // If we could get a response
        var responseBody    = response.body;                    // Drop connection information 
        var volumeInfo      = responseBody.items[0].volumeInfo; // Get information of first match
        return volumeInfo;
    } else {
        System.print("Failed to load " :: isbn);
    }
}

// Prints title, author and number of pages
function printBookInformation(book){
    System.print(book.title :: " by: " :: String.join(book.authors, ", ") :: ", pages: " :: book.pageCount);   
}

 

 

 Another often used feature is the Post function. This is used for sending data to a server. It can be used to create new items, but also to fill out forms and get the result. The simple example below shows how to send a Post request to a server. It also prints the response of the server, containing the status code (if it request succeeded) and additional information about the connection.


 

use XURL, System;

var response = XURL.post(
    "https://httpbin.org/post",
    "This is my plain text data"
);

var prettyJson = System.toJSON(response, true);
System.print(prettyJson);