{ DreamFactory: 'Blog' }

GET this! 7 simple REST client examples for retrieving API data.

Posted by Matthew Schaer

Thu, Dec 8, 2016

So you’ve installed DreamFactory and connected to your database, maybe you’ve tried a few API calls in API docs but what’s next? Certainly not writing the server-side code since DreamFactory graciously autogenerated your API endpoints. How about a REST client, perhaps we’ll start with a simple GET? Yeah! That sounds like a safe bet!

Simply put a GET request is a request for a representation of a resource, resource being the URL and the representation being the data. GET is considered safe since it won’t change the state of the resource.

Below we will demonstrate 7 simple examples of REST clients performing a GET request to DreamFactory using only an API key for authentication. We will cover some common languages and libraries for getting started with GET requests. In the coming weeks and months we will expand on this topic to more complex API calls, more advanced authentication techniques as well as introduce some new languages, but for now let’s keep it simple.

We will be using the data from the DreamFactory sample apps so if you haven’t done so already in your DreamFactory click on the ‘apps’ tab. On the left side of the screen click ‘import’ and ‘Select’ next to the ‘Address Book for JavaScript’, scroll to of the bottom of the screen and click the blue ‘import’ button. Once you’ve verified the sample app has imported successfully cruise over to API docs and do:

GET /db/_schema

db.gif

If the GET request executes successfully you should receive a Response Code of ‘200’ indicating success, as well as a JSON representation of the tables in your schema. Now that we’ve got some data to use let’s take a look at some GET requests. In our examples we will be using the ‘contact_info’ table as follows:

GET http://localhost/api/v2/db/_table/contact_info

For our first example we will look at two simple NodeJS scripts. Below is an example of a native NodeJS HTTP GET request. In your favorite text editor create a new file called ‘rest.js’ and enter the following code:

node1.png

Save the file as ‘rest.js’ and navigate to the directory in your shell where the file has been saved and run:

             node rest.js    

nd.gif

 

Dig it! You should see a whole bunch of JSON data returned after the script is executed if you’d like to clean it up and look a little more closely at your data you can copy your response and slide over to jsonlint.com and paste the response in to the window and click ‘validate JSON’ which will do two things:

  1. Format your JSON into something a little more human readable.
  2. Validate that the JSON is in correct format as described by the JSON spec. If for any reason the JSON isn’t valid you will be given a hint as to what steps to take to correctly format your JSON.

I use jsonlint.com almost daily and you should too!

That script felt a little heavy handed for just one simple GET call. Let’s try the same call again but this time we will use the ‘unirest’ NPM module. NPM comes with NodeJS and allows users to install what are called Node modules. If you plan on doing any custom scripted services or events scripting in your DreamFactory this will also be useful. Let’s install the ‘unirest’ module using NPM, open up a shell and type:

npm install -g unirest

The -g option installs the module globally, we recommend installing NodeJS modules globally, especially if you intend to use them in your server-side scripts. Now that you’ve installed the ‘unirest’ module open up your text editor and type the following code:

node2.png

Save the script as ‘resty.js’ and navigate to the directory where the scripts is saved and type:

node resty.js

nd2.gif

So we just made the same GET as before but this time with a lot less code and our JSON response was formatted, sweet!

Maybe NodeJS isn’t your cup of tea, maybe you’re more of a Python guy or gal. Python makes REST a cakewalk with the ‘requests’ module. Let’s install the ‘requests’ module using pip,the Python package manager. Open up your terminal and type:

pip install requests

Now that we’ve got the ‘requests’ module installed let’s again open up our favorite text editor and enter the following code:

python.png

Once you’ve confirmed everything is correct save the file as ‘rest.py’ and again navigate to the directory where you saved your script and type:

python rest.py

py.gif

So far so good, let’s try a few more examples!

PHP? No problem! Jump back in the text editor and enter the following code and save the file as ‘rest.php’:

php1.png

Save the file and navigate to where you’ve saved ‘rest.php’ script and type:

php rest.php

php.gif

Ruby has more HTTP client libraries then I can count on my fingers and toes. Below we will cover two. Let’s first make a call using ‘net/http’ which is built into the Ruby standard library. In your text editor type the following code and save the script as ‘rest.rb’:

ruby1.png

Save the file and navigate to where you saved ‘rest.rb’ and type the following:

ruby rest.rb

rb1.gif

Now let’s try this again, however this time we are going to use the Ruby ‘httparty’ gem. This incredibly useful Ruby gem which claims to “make http fun again” can be used interactively in the command line or within your Ruby, Sinatra and Ruby on Rails applications. Lets use Ruby’s package manager ‘Gem’ to install the ‘httparty’ gem. In the terminal type:

gem install httparty

Once you’ve installed the ‘httparty’ gem open up your text editor and type in the following code and save the script as a file called ‘resty.rb’:

fixed.png

Save the file and navigate to where you saved ‘resty.rb’ and type the following:

ruby resty.rb

rb2.gif

Just to mix it up let’s do the same GET request in Perl. Using ‘cpan’ install the ‘REST::Client’ Perl module. Open up your terminal and type:

cpan

Once in the cpan shell type:

install REST::Client

After ‘REST::Client’ finishes installing exit the cpan shell and open your text editor and enter the following code, save the script as ‘rest.pl’:

perl.png

Save the file and navigate to where you saved ‘rest.pl’ and type the following:

perl rest.pl

nd.gif

Boom! So there you have it, 7 quick and easy scripts to GET and parse data from the DreamFactory REST API. Something else you may have noticed is that the API endpoint was used over and over again in each of the client calls without requiring a single line of server-side code, awesome!

If there is a topic you’d like us to cover feel free to tweet us @dfsoftwareinc and let us know! We would love to hear from you! We will continue to blog about different topics over the coming months so stay tuned!

If you haven’t had an opportunity to try DreamFactory yet we have a number of options to get you up and running on the DreamFactory platform and offer 10 days free support to help you get started! To learn more head over to: https://www.dreamfactory.com/

Get started with DreamFactory with a free hosted DreamFactory development environment . Or, download and run it on the server, cloud, or desktop of your choice.

Tutorials Python Ruby RESTClient NodeJS

Weekly Digest