SlideShare a Scribd company logo
1 of 26
An Introduction to
REST API
Presented By:
Aniruddh Bhilvare
22 April, 2017
What is it ?
API
WEB
URL
HTTP
GET
INTERNET
JSON
REQUEST
HEADER
POST
RESPONSE
METHOD
RESPONSE
What is it ?
REST means
REpresentational
State
Transfer
REpresentational ? State ? Transfer ?
It represent the state of database at a time. But how???
REST is an architectural style which is based on web-standards and the
HTTP protocol.
In a REST based architecture everything is a Resource.
A resource is accessed via a common interface based on the HTTP standard
methods.
You typically have a REST server which provides access to the resources and
a REST client which accesses and modifies the REST resources.
REpresentational ? State ? Transfer ?
Every resource should support the HTTP common operations.
Resources are identified by global IDs (which are typically URIs or URLs).
REST allows that resources have different representations, e.g., text, XML,
JSON etc.
Stateless in nature. Excellent for distributed system.
Stateless components can be freely redeployed if something fails, and they
can scale to accommodate load changes.
This is because any request can be directed to any instance of a component.
HTTP Methods
The PUT, GET, POST and DELETE methods are typically used in REST based
architectures. The following table gives an explanation of these operations:
HTTP Method CRUD Operation Description
POST INSERT Addes to an existing resource
PUT UPDATE Overrides existing resource
GET SELECT Fetches a resource. The resource is
never changed via a GET request
DELETE DELETE Deletes a resource
Architecture:
HTTP Request Example:
HTTP Response Example:
HTTP REST Request:
HTTP REST API Request
GET https://www.myhost.com/api/v1/user/1/cities
Read, All the cities for user whose id is 1
GET /user/1/cities http/1.1
host: https://www.myhost.com/api/v1
Content-Type: application/json
Accept-Language: us-en
state_id: 2
HTTP REST Response:
HTTP REST API Response
HTTP/1.1 200 OK (285ms)
Date: Fri, 21 Apr 2017 10:27:20 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips PHP/7.0.16
X-Powered-By: PHP/7.0.16
Content-Length: 109
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/json; charset=UTF-8
{"status":"success","message":"City
List","data":[{"city_name":"Visakhapatnam"},{"city_name":"Vijayawada"}]}
HTTP Response Status Code:
1xx Informational Codes
2xx Successful Codes
3xx Redirection Codes
4xx Client Error Code
5xx Server Error Codes
List at here: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
Points to be noted
For REST APIs
Use Nouns but no verbs in path/URI:
Purpose Method Incorrect Correct
Retrieves a list of users GET /getAllCars /users
Create a new user POST /createUser /users
Delete a user DELETE /deleteUser /users/10
Get balance of user GET /getUserBalance /users/11/balance
Use plural nouns:
Do not mix up singular and plural nouns. Keep it simple and use only plural
nouns for all resources.
/cars instead of /car
/users instead of /user
/products instead of /product
/settings instead of /setting
GET method should not alter the state:
Use PUT, POST and DELETE methods instead of the GET method to alter
the state.
Do not use GET method or Query parameters for state changes:
GET /users/711?activate or
GET /users/711/activate
Use sub-resources for relation:
If a resource is related to another resource use subresources
GET /cars/711/drivers/ (Returns a list of drivers for car 711)
GET /cars/711/drivers/4 (Returns driver #4 for car 711)
Use HTTP headers for serialization formats:
Both, client and server need to know which format is used for the
communication. The format has to be specified in the HTTP-Header.
Content-Type defines the request format.
Accept defines a list of acceptable response formats.
Versioning is important:
Make the API Version mandatory and do not release an unversioned API.
Use a simple ordinal number and avoid dot notation such as 2.5
We are using the url for the API versioning starting with the letter “v”
/blog/api/v1
Handle Errors with HTTP Status code:
200 OK (Everything is working) 403 Forbidden (The server understood the
request, but is refusing it or the access is
not allowed)
201 OK (New resource has been created) 404 Not found (There is no resource behind the
URI)
204 OK (Resource successfully deleted) 405 Method not allowed
400 Bad Request (The request was invalid or
cannot be served. The exact error should
be explained in the error payload. E.g.
„The JSON is not valid“)
408 Request timeout
401 Unauthorized (The request requires an
user authentication)
500 Internal server error
Filtering:
Use a unique query parameter for all fields or a query language for filtering.
GET /cars?color=red (Returns a list of red cars)
GET /users?name=tom (Returns a list of users whose name matches tom)
Sorting:
Allow ascending and descending sorting over multiple fields.
GET /cars?sort=-manufacturer,+model
This returns a list of cars sorted by descending manufacturers and
ascending models)
Paging:
Use limit and offset. It is flexible for the user and common in leading
databases.
The default should be limit=20 and offset=0
GET /cars?offset=10&limit=5
Aliases for common queries:
To make the API experience more pleasant for the average consumer,
consider packaging up sets of conditions into easily accessible RESTful
paths.
For example consider following use case:
POST /users (User Register: Creates a new user)
POST /users/login (User Login: Creates auth token for user authentication)
Want to develop REST APIs easily???
Checkout Slim - A microframework for php.
Documentation: https://www.slimframework.com/docs/
Thanks for your Time & Patience!
Any Question?

More Related Content

What's hot

Introduction to the Web API
Introduction to the Web APIIntroduction to the Web API
Introduction to the Web APIBrad Genereaux
 
REST API Design & Development
REST API Design & DevelopmentREST API Design & Development
REST API Design & DevelopmentAshok Pundit
 
REST-API introduction for developers
REST-API introduction for developersREST-API introduction for developers
REST-API introduction for developersPatrick Savalle
 
Introduction to APIs (Application Programming Interface)
Introduction to APIs (Application Programming Interface) Introduction to APIs (Application Programming Interface)
Introduction to APIs (Application Programming Interface) Vibhawa Nirmal
 
Web Services PHP Tutorial
Web Services PHP TutorialWeb Services PHP Tutorial
Web Services PHP TutorialLorna Mitchell
 
Rest api standards and best practices
Rest api standards and best practicesRest api standards and best practices
Rest api standards and best practicesAnkita Mahajan
 
Introduction to REST - API
Introduction to REST - APIIntroduction to REST - API
Introduction to REST - APIChetan Gadodia
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.jsVikash Singh
 
Introduction to API
Introduction to APIIntroduction to API
Introduction to APIrajnishjha29
 
Introduction to Swagger
Introduction to SwaggerIntroduction to Swagger
Introduction to SwaggerKnoldus Inc.
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepGuo Albert
 

What's hot (20)

Introduction to the Web API
Introduction to the Web APIIntroduction to the Web API
Introduction to the Web API
 
REST & RESTful Web Services
REST & RESTful Web ServicesREST & RESTful Web Services
REST & RESTful Web Services
 
Api presentation
Api presentationApi presentation
Api presentation
 
REST API Design & Development
REST API Design & DevelopmentREST API Design & Development
REST API Design & Development
 
API for Beginners
API for BeginnersAPI for Beginners
API for Beginners
 
REST-API introduction for developers
REST-API introduction for developersREST-API introduction for developers
REST-API introduction for developers
 
Api types
Api typesApi types
Api types
 
Introduction to APIs (Application Programming Interface)
Introduction to APIs (Application Programming Interface) Introduction to APIs (Application Programming Interface)
Introduction to APIs (Application Programming Interface)
 
Web services SOAP
Web services SOAPWeb services SOAP
Web services SOAP
 
Web Services PHP Tutorial
Web Services PHP TutorialWeb Services PHP Tutorial
Web Services PHP Tutorial
 
Rest api standards and best practices
Rest api standards and best practicesRest api standards and best practices
Rest api standards and best practices
 
RESTful API - Best Practices
RESTful API - Best PracticesRESTful API - Best Practices
RESTful API - Best Practices
 
Introduction to REST - API
Introduction to REST - APIIntroduction to REST - API
Introduction to REST - API
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Introduction to API
Introduction to APIIntroduction to API
Introduction to API
 
Api Testing
Api TestingApi Testing
Api Testing
 
Introduction to Swagger
Introduction to SwaggerIntroduction to Swagger
Introduction to Swagger
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
 
API for Beginners
API for BeginnersAPI for Beginners
API for Beginners
 
Spring Security 5
Spring Security 5Spring Security 5
Spring Security 5
 

Similar to An Introduction To REST API

REST library.pptx
REST library.pptxREST library.pptx
REST library.pptxMSivani
 
RESTful services
RESTful servicesRESTful services
RESTful servicesgouthamrv
 
REST API in Agile Testing by Fadly Mahendra
REST API in Agile Testing by Fadly MahendraREST API in Agile Testing by Fadly Mahendra
REST API in Agile Testing by Fadly MahendraFadly Mahendra
 
Writing RESTful Web Services
Writing RESTful Web ServicesWriting RESTful Web Services
Writing RESTful Web ServicesPaul Boocock
 
A Conversation About REST - Extended Version
A Conversation About REST - Extended VersionA Conversation About REST - Extended Version
A Conversation About REST - Extended VersionJeremy Brown
 
HTTP Request and Response Structure
HTTP Request and Response StructureHTTP Request and Response Structure
HTTP Request and Response StructureBhagyashreeGajera1
 
Creating Restful Web Services with restish
Creating Restful Web Services with restishCreating Restful Web Services with restish
Creating Restful Web Services with restishGrig Gheorghiu
 
A Conversation About REST
A Conversation About RESTA Conversation About REST
A Conversation About RESTJeremy Brown
 
A Conversation About REST
A Conversation About RESTA Conversation About REST
A Conversation About RESTMike Wilcox
 
ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiTiago Knoch
 

Similar to An Introduction To REST API (20)

Salesforce Integration
Salesforce IntegrationSalesforce Integration
Salesforce Integration
 
WebApp #3 : API
WebApp #3 : APIWebApp #3 : API
WebApp #3 : API
 
Salesforce REST API
Salesforce  REST API Salesforce  REST API
Salesforce REST API
 
REST library.pptx
REST library.pptxREST library.pptx
REST library.pptx
 
Apitesting.pptx
Apitesting.pptxApitesting.pptx
Apitesting.pptx
 
RESTful services
RESTful servicesRESTful services
RESTful services
 
REST API in Agile Testing by Fadly Mahendra
REST API in Agile Testing by Fadly MahendraREST API in Agile Testing by Fadly Mahendra
REST API in Agile Testing by Fadly Mahendra
 
Writing RESTful Web Services
Writing RESTful Web ServicesWriting RESTful Web Services
Writing RESTful Web Services
 
ASP.NET WEB API Training
ASP.NET WEB API TrainingASP.NET WEB API Training
ASP.NET WEB API Training
 
API Basics
API BasicsAPI Basics
API Basics
 
A Conversation About REST - Extended Version
A Conversation About REST - Extended VersionA Conversation About REST - Extended Version
A Conversation About REST - Extended Version
 
Restful web services
Restful web servicesRestful web services
Restful web services
 
HTTP Request and Response Structure
HTTP Request and Response StructureHTTP Request and Response Structure
HTTP Request and Response Structure
 
Creating Restful Web Services with restish
Creating Restful Web Services with restishCreating Restful Web Services with restish
Creating Restful Web Services with restish
 
Standards of rest api
Standards of rest apiStandards of rest api
Standards of rest api
 
A Conversation About REST
A Conversation About RESTA Conversation About REST
A Conversation About REST
 
A Conversation About REST
A Conversation About RESTA Conversation About REST
A Conversation About REST
 
API Testing Basics.pptx
API Testing Basics.pptxAPI Testing Basics.pptx
API Testing Basics.pptx
 
A Look at OData
A Look at ODataA Look at OData
A Look at OData
 
ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web api
 

Recently uploaded

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 

Recently uploaded (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 

An Introduction To REST API

  • 1. An Introduction to REST API Presented By: Aniruddh Bhilvare 22 April, 2017
  • 2. What is it ? API WEB URL HTTP GET INTERNET JSON REQUEST HEADER POST RESPONSE METHOD RESPONSE
  • 3. What is it ? REST means REpresentational State Transfer
  • 4. REpresentational ? State ? Transfer ? It represent the state of database at a time. But how??? REST is an architectural style which is based on web-standards and the HTTP protocol. In a REST based architecture everything is a Resource. A resource is accessed via a common interface based on the HTTP standard methods. You typically have a REST server which provides access to the resources and a REST client which accesses and modifies the REST resources.
  • 5. REpresentational ? State ? Transfer ? Every resource should support the HTTP common operations. Resources are identified by global IDs (which are typically URIs or URLs). REST allows that resources have different representations, e.g., text, XML, JSON etc. Stateless in nature. Excellent for distributed system. Stateless components can be freely redeployed if something fails, and they can scale to accommodate load changes. This is because any request can be directed to any instance of a component.
  • 6. HTTP Methods The PUT, GET, POST and DELETE methods are typically used in REST based architectures. The following table gives an explanation of these operations: HTTP Method CRUD Operation Description POST INSERT Addes to an existing resource PUT UPDATE Overrides existing resource GET SELECT Fetches a resource. The resource is never changed via a GET request DELETE DELETE Deletes a resource
  • 10. HTTP REST Request: HTTP REST API Request GET https://www.myhost.com/api/v1/user/1/cities Read, All the cities for user whose id is 1 GET /user/1/cities http/1.1 host: https://www.myhost.com/api/v1 Content-Type: application/json Accept-Language: us-en state_id: 2
  • 11. HTTP REST Response: HTTP REST API Response HTTP/1.1 200 OK (285ms) Date: Fri, 21 Apr 2017 10:27:20 GMT Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips PHP/7.0.16 X-Powered-By: PHP/7.0.16 Content-Length: 109 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Content-Type: application/json; charset=UTF-8 {"status":"success","message":"City List","data":[{"city_name":"Visakhapatnam"},{"city_name":"Vijayawada"}]}
  • 12. HTTP Response Status Code: 1xx Informational Codes 2xx Successful Codes 3xx Redirection Codes 4xx Client Error Code 5xx Server Error Codes List at here: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
  • 13. Points to be noted For REST APIs
  • 14. Use Nouns but no verbs in path/URI: Purpose Method Incorrect Correct Retrieves a list of users GET /getAllCars /users Create a new user POST /createUser /users Delete a user DELETE /deleteUser /users/10 Get balance of user GET /getUserBalance /users/11/balance
  • 15. Use plural nouns: Do not mix up singular and plural nouns. Keep it simple and use only plural nouns for all resources. /cars instead of /car /users instead of /user /products instead of /product /settings instead of /setting
  • 16. GET method should not alter the state: Use PUT, POST and DELETE methods instead of the GET method to alter the state. Do not use GET method or Query parameters for state changes: GET /users/711?activate or GET /users/711/activate
  • 17. Use sub-resources for relation: If a resource is related to another resource use subresources GET /cars/711/drivers/ (Returns a list of drivers for car 711) GET /cars/711/drivers/4 (Returns driver #4 for car 711)
  • 18. Use HTTP headers for serialization formats: Both, client and server need to know which format is used for the communication. The format has to be specified in the HTTP-Header. Content-Type defines the request format. Accept defines a list of acceptable response formats.
  • 19. Versioning is important: Make the API Version mandatory and do not release an unversioned API. Use a simple ordinal number and avoid dot notation such as 2.5 We are using the url for the API versioning starting with the letter “v” /blog/api/v1
  • 20. Handle Errors with HTTP Status code: 200 OK (Everything is working) 403 Forbidden (The server understood the request, but is refusing it or the access is not allowed) 201 OK (New resource has been created) 404 Not found (There is no resource behind the URI) 204 OK (Resource successfully deleted) 405 Method not allowed 400 Bad Request (The request was invalid or cannot be served. The exact error should be explained in the error payload. E.g. „The JSON is not valid“) 408 Request timeout 401 Unauthorized (The request requires an user authentication) 500 Internal server error
  • 21. Filtering: Use a unique query parameter for all fields or a query language for filtering. GET /cars?color=red (Returns a list of red cars) GET /users?name=tom (Returns a list of users whose name matches tom)
  • 22. Sorting: Allow ascending and descending sorting over multiple fields. GET /cars?sort=-manufacturer,+model This returns a list of cars sorted by descending manufacturers and ascending models)
  • 23. Paging: Use limit and offset. It is flexible for the user and common in leading databases. The default should be limit=20 and offset=0 GET /cars?offset=10&limit=5
  • 24. Aliases for common queries: To make the API experience more pleasant for the average consumer, consider packaging up sets of conditions into easily accessible RESTful paths. For example consider following use case: POST /users (User Register: Creates a new user) POST /users/login (User Login: Creates auth token for user authentication)
  • 25. Want to develop REST APIs easily??? Checkout Slim - A microframework for php. Documentation: https://www.slimframework.com/docs/
  • 26. Thanks for your Time & Patience! Any Question?