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.
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:
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:
Save the file as ‘rest.js’ and navigate to the directory in your shell where the file has been saved and run:
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 and paste the response in to the window and click ‘validate JSON’ which will do two things:
- Format your JSON into something a little more human readable.
- 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 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:
Save the script as ‘resty.js’ and navigate to the directory where the scripts is saved and type:
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:
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:
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’:
Save the file and navigate to where you’ve saved ‘rest.php’ script and type:
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’:
Save the file and navigate to where you saved ‘rest.rb’ and type the following:
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’:
Save the file and navigate to where you saved ‘resty.rb’ and type the following:
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:
Once in the cpan shell type:
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’:
Save the file and navigate to where you saved ‘rest.pl’ and type the following:
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 give the button below a click.