Working with RESTful services in Codeigniter – POST example

In this tutorial I am going to show you how we can work with RESTful or REST webservice in Codeigniter framework.

You may also like the Working with RESTful services in Codeigniter – PUT example, Codeigniter GET example with REST service and Working with RESTful services in Codeigniter – DELETE example


The most important concept in REST is resources, which are identified by global IDs — typically using URIs. Client applications use HTTP methods (GET/ POST/ PUT/ DELETE) to manipulate the resource or collection of resources. A RESTful Web service is implemented using HTTP and the principles of REST. Typically, a RESTful Web service should define the following aspects:

The base/root URI for the Web service such as http://<host>/<appcontext/contextpath>/<url pattern>/<resources>.
The MIME type of the response data supported, which are JSON/XML/TEXT/HTML etc.
The set of operations supported by the service. (for example, POST, GET, PUT or DELETE).


HTTP methods are mapped to CRUD (create, read, update and delete) actions for a resource. Although you can make slight modifications such as making the PUT method to create or update, the basic patterns are listed as follows.

HTTP GET: Get/List/Retrieve an individual resource or a collection of resources.
HTTP POST: Create a new resource or resources.
HTTP PUT: Update an existing resource or collection of resources.
HTTP DELETE: Delete a resource or collection of resources.

You may read the section on REST API for Codeigniter


Please go through Setup RESTful service with Codeigniter


Step 1. Create a MySQL table contact under database roytuts

Step 2. Modify <root directory>/application/config/autoload.php file for auto-loading html, url, file, form and database

Step 3. Modify also <root directory>/application/config/database.php

Step 4. Create a controller file RestPostController.php under <project root>/application/controllers with the following source code

  • is an alias for $this->input->post() which is CodeIgniter’s method to access $_POST variables with XSS protection.

Step 5. Create a model file ContactModel.php under <project root>/application/models with the following source code

Step 6. Modify file <project root>/application/config/routes.php file

Step 7. Now run the application on apache http server.

Step 8. Use the REST Client in Firefox browser to test the REST service.

Method: POST

Headers: Add Custom Header as name = Content-Type, value=application/json

URL: http://localhost/ci_3_1_0/index.php/restpostcontroller/add_contact

Body: {“name”:”S Roy”, “address”:””, “phone”:”1234567890″}

$[‘rest_default_format’]=’json’ at <root directory>/application/config/rest.php


Step 9. Check the database table, you will see one new row has been added with below data

Thanks for reading.

One Reply to “Working with RESTful services in Codeigniter – POST example”

  1. Thank you so much! I’m just starting to work with APIs and this tutorial was really useful for me. Really well explained. Thanks again.

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.