How to write a REST API?

EDIT:

The links below which apparently were good for 3 years are no longer working so I went and found a couple of new tutorials that I think are going to stick around for a while. These are on the Ray Wenderlich site, a very well respected ios dev tutorial site. The first article actually references the broken links below but it is complete within itself:

How To Write A Simple PHP/MySQL Web Service for an iOS App

and the second one has a little twist to it. It used parse.com on the backend and AFNetworking. Both of which are quite excellent.

How To Synchronize Core Data with a Web Service – Part 1


I have fixed the broken links below by finding the articles in the way back machine. People seem to like the links so I will keep them. The links above should provide more food for thought.


I am doing exactly the same thing with my iphone app. I found this article on building a RESTful API in PHP:

https://web.archive.org/web/20130910164802/http://www.gen-x-design.com/archives/create-a-rest-api-with-php/

and there is also a followup article here:

https://web.archive.org/web/20130323001500/http://www.gen-x-design.com/archives/making-restful-requests-in-php/

with a link to source code at the bottom of the article.


I have programmed a REST API in ZEND Framework using the Zend_Rest_Controller, on the iPhone I used ASIHTTPRequest. My experience with both where good. At the beginning I had some trouble setting up ZEND and connecting it to mySQL, but once I figured out how to do it I was able to write the API very quickly. I can share more information with you if you have any further questions.

EDIT: There seems to be no official documentation on Zend_Rest_Controller. This link describes how to use it to create your API. You simply have to disable rendering in the init() of your subclass and implement the methods for each REST call.


Just to let you know:

I ended up using Ruby on Rails.

EDIT: Since this answer has been downvoted for not providing the reason behind choosing Ruby on Rails and also no instructions on how to write a REST API with it, I thought I would give you my motivation and some simple instructions.

I started reading a book about Ruby on Rails and realized that all I needed to do was to use scaffolding and I got a JSON REST API for free.

Here's a good guide to get you started: http://guides.rubyonrails.org/getting_started.html

When you have your Ruby on Rails environment up and running, creating your REST API isn't harder than running:

$ rails generate scaffold Post name:string title:string content:text

(Example from the above link.) I also found that Rails is very easy and free to deploy to heroku, which meant that I didn't have to pay for hosting for my very basic, low traffic, REST API. There are many other reasons why I am very happy to work with Ruby on Rails, but that's beyond the context of this question.


I followed a quite simple tutorial for creating RESTful APIs with PHP:

Corey Maynard - Creating a RESTful API with PHP

The main concept includes:

  • one abstract class that handles the parsing of the URI and returning the response, and
  • one concrete class that consists of just the endpoints for the API.

What about Python?

I'd use Python, Django and Piston.

  1. I'd generate Django models from your existent DB using inspectdb.
  2. Add the Django admin to your models.
  3. Add Django Piston to your app.
  4. Profit.

With no experience with Python or Django probably it'll take you a day to develop this solution and all code is unit tested and proved to work.