Working with REST

An introduction on the server technology and how to connect your app to a web endpoint
This is the first article, in a series.

By the end of the series, you should be able to connect your app, whether macOS, iOS, console, to a REST endpoint.

There are a lot of articles on the internet that cover connecting to a web end, this is a little different in that it takes you from baby steps to running.

From really easy, to really hard.

Like any other journey, let’s start at a beginning, with a foundation.

The languages used in this series will be Swift, and C++; the articles will try to make the code as close to each other when possible.

That way, when you are switching between the articles, you shouldn’t be too confused.

With all this said, let’s get to the first step, which is setting up some tools for fast testing.

Fast testing

To test your REST code, you will need a server that will respond to HTTP POST and GET queries.

You could set up a local apache server or use a service provider like Twitter, Google or Flickr, but we will be using a simpler method.

The tool we will use is called json-server (json-server on github).

You can install the server on macOS using npm:

$npm install -g json-server

The npm tools the Node Package Manager. If you don’t have it already installed (you can check by typing $npm -v in the command line), you can install the tool using Homebrew:

$brew install node

After Homebrew is done installing Node, double check to make sure it’s installed:

$node -v
$npm -v

If you had to install Node, re-run the command to install json-server.

At this point, you should have a JSON server installed on your computer.

Now get things going, create a JSON file, in whichever directory you would like to place it in.

The location doesn’t matter so much, as long as it is accessible by your computer.

For my testing, I created a JSON file named db.json with the following contents:

  "events": [{
      "id": 1,
      "date_from": "2019-06-03",
      "date_to": "2019-06-07",
      "name": "WWDC 2019",
      "where": "San Jose McEnery Convention Center, San Jose, CA"
      "id": 2,
      "date_from": "2019-09-12",
      "date_to": "2019-09-20",
      "name": "CPPCon 2019",
      "where": "Aurora, Colorado, USA"

I used to validate the above JSON contents. I recommend you do the same with your test JSON.

Let’s start up the json-server; open up your favorite terminal app, cd to the directory where your JSON file is, and invoke the following command:

$json-server --watch db.json

You should get the following response:

\{^_^}/ hi!

Loading db.json



Type s + enter at any time to create a snapshot of the database

Open up your web browser of choice, then go to http://localhost:3000 to see the contents of the JSON file.

With that done, let’s move on to the next step, creating an app to connect to the local JSON server.


Article Series Index

Leave a Reply

Your email address will not be published. Required fields are marked *