KEMBAR78
REST API Basics | PDF
Lecture Series by
Tharindu Weerasinghe
For the 3rd Year Undergrads of APIIT – Sri Lanka
www.tharinduweerasinghe.com
REST API Basics
Outline
• REST API Introduction
• REST API Action Flow
• HTTP Methods
• Sample usage of the HTTP Methods in the Requests
• Example API Requests
• 3 steps to design API end-points
• Testing REST End-Points
www.tharinduweerasinghe.com
REST API Introduction
• REST ➔ REpresentational State Transfer (REST)
• API ➔ Application Programming Interface
• It can be regarded as a protocol that defines a set of constraints to be
used for creating web services.
• It is a way of accessing web services in a less complex manner without
having any processing.
• It caters only HTTP (Hypertext Transfer Protocol) requests.
• It’s a medium for two computers to communicate over HTTP in the
same way clients and servers communicate.
www.tharinduweerasinghe.com
REST API Action Flow
• Credit:
https://faun.pub/consumin
g-rest-apis-with-python-
eb86c6b724c5
www.tharinduweerasinghe.com
HTTP Methods used in REST APIs
1. GET: The HTTP GET method is used to retrieve a representation of a resource. In the successful path, GET returns a representation in XML or JSON and
an HTTP response code of 200 (OK). In the erroneous situation, it most often returns a 404 (NOT FOUND) or 400 (BAD REQUEST).
2. POST: The POST method is used to create new resources. In particular, it is utilized to create subordinate resources. That is, subordinate to some other
(e.g. parent) resource. On successful creation, return HTTP status 201, returning a Location header with a link to the newly-created resource with the
201 HTTP status.
NOTE: POST is neither safe nor idempotent.
3. PUT: The PUT method for updating the capabilities. However, PUT can also be used to create a resource in the case where the resource ID is chosen
by the client instead of by the server. In other words, if the PUT is to a URI that contains the value of a non-existent resource ID. On successful update,
return 200 (or 204 if not returning any content in the body) from a PUT. If using PUT for create, return HTTP status 201 on successful creation. PUT is
not safe operation but it’s idempotent.
4. PATCH: The PATCH method used for modify capabilities. The PATCH request only needs to contain the changes to the resource, not the complete
resource. This resembles PUT, but the body contains a set of instructions describing how a resource currently residing on the server should be
modified to produce a new version. This means that the PATCH body should not just be a modified part of the resource, but in some kind of patch
language like JSON Patch or XML Patch. PATCH is neither safe nor idempotent.
5. DELETE: The DELETE method is used to delete a resource identified by a URI. On successful deletion, return HTTP status 200 (OK) along with a
response body.
Credits: REST API (Introduction) - GeeksforGeeks
www.tharinduweerasinghe.com
www.tharinduweerasinghe.com
Sample usage of the HTTP Methods in the Requests
Credits: https://assertible.com/blog/7-http-methods-every-web-developer-should-know-and-how-to-test-them
HTTP Status Codes
• Credits:
https://faun.pub/consuming-
rest-apis-with-python-
eb86c6b724c5
www.tharinduweerasinghe.com
Example:
Calling a REST API End-Point in Javascript
www.tharinduweerasinghe.com
The following slide will show you how can you
include your JavaScript code shown in the
previous slide in an HTML code!
www.tharinduweerasinghe.com
www.tharinduweerasinghe.com
3 steps to design API end-points using Python
• Step 1: Design your API Interface
• The most vital step
• You need to know the problem you want to address for a successful design
• Basically, this will cater the business requirements of sending relevant data for respective HTTP
requests
• You need to decide on the format of the request and response
• Use API URL best practices in designing your APIs.
• You need to describe your REST APIs using OpenAPI definition, designing an API means creating
an OpenAPI document for it.
Credit: https://blog.stoplight.io/how-to-create-an-api-in-three-steps
www.tharinduweerasinghe.com
3 steps to design API end-points using Python
(contd.)
• Step 2: Mock your API Server
• Once you have your design, you need to check the manipulated response.
• After you create your OpenAPI document for your new API, create a mock server. There are a few
ways to do this, one of the methods is to use an open-source command-line utility like the Prism API
server.
www.tharinduweerasinghe.com
Credit: https://blog.stoplight.io/how-to-create-an-api-in-three-steps
3 steps to design API end-points using Python
(contd.)
• Step 3: Code your API
• Once you have your OpenAPI
document then, you can select
whatever the platform or
language you would like to
code your API and build the
API server. Here I’m using
Python with a library called
Flask.
www.tharinduweerasinghe.com
Credit: https://blog.stoplight.io/how-to-create-an-api-in-three-steps
REST API Testing Tools
• Postman
• Swagger
https://www.softwaretestingmaterial.com/best-api-testing-tools/
Reference
• Understanding And Using REST APIs — Smashing Magazine
• REST API (Introduction) – GeeksforGeeks
• Python and REST APIs: Interacting With Web Services – Real Python
• 7 Simple REST Client Examples for Retrieving API Data (dreamfactory.com)
• REST API Example Requests – Tableau
• Building a Basic RestFul API in Python | Codementor
• REST API Testing Strategy: What Exactly Should You Test? l Sisense
• Online API Testing Tool | Test Your API Online (reqbin.com)
• Postman API Platform | Sign Up for Free
• SwaggerHub | API Design and Documentation with OpenAPI
• Python REST API Tutorial - Building a Flask REST API – YouTube
• https://levelup.gitconnected.com/all-possible-ways-of-making-an-api-call-in-plain-javascript-c0dee3c11b8b
• https://www.testim.io/blog/the-9-api-testing-tools-you-cant-live-without-in-2019-2/
• https://www.freecodecamp.org/news/rest-api-best-practices-rest-endpoint-design-examples/
• https://www.freecodecamp.org/news/rest-api-best-practices-rest-endpoint-design-examples/
• https://rapidapi.com/blog/how-to-use-an-api-with-javascript/
• https://www.softwaretestingmaterial.com/best-api-testing-tools/
www.tharinduweerasinghe.com

REST API Basics

  • 1.
    Lecture Series by TharinduWeerasinghe For the 3rd Year Undergrads of APIIT – Sri Lanka www.tharinduweerasinghe.com REST API Basics
  • 2.
    Outline • REST APIIntroduction • REST API Action Flow • HTTP Methods • Sample usage of the HTTP Methods in the Requests • Example API Requests • 3 steps to design API end-points • Testing REST End-Points www.tharinduweerasinghe.com
  • 3.
    REST API Introduction •REST ➔ REpresentational State Transfer (REST) • API ➔ Application Programming Interface • It can be regarded as a protocol that defines a set of constraints to be used for creating web services. • It is a way of accessing web services in a less complex manner without having any processing. • It caters only HTTP (Hypertext Transfer Protocol) requests. • It’s a medium for two computers to communicate over HTTP in the same way clients and servers communicate. www.tharinduweerasinghe.com
  • 4.
    REST API ActionFlow • Credit: https://faun.pub/consumin g-rest-apis-with-python- eb86c6b724c5 www.tharinduweerasinghe.com
  • 5.
    HTTP Methods usedin REST APIs 1. GET: The HTTP GET method is used to retrieve a representation of a resource. In the successful path, GET returns a representation in XML or JSON and an HTTP response code of 200 (OK). In the erroneous situation, it most often returns a 404 (NOT FOUND) or 400 (BAD REQUEST). 2. POST: The POST method is used to create new resources. In particular, it is utilized to create subordinate resources. That is, subordinate to some other (e.g. parent) resource. On successful creation, return HTTP status 201, returning a Location header with a link to the newly-created resource with the 201 HTTP status. NOTE: POST is neither safe nor idempotent. 3. PUT: The PUT method for updating the capabilities. However, PUT can also be used to create a resource in the case where the resource ID is chosen by the client instead of by the server. In other words, if the PUT is to a URI that contains the value of a non-existent resource ID. On successful update, return 200 (or 204 if not returning any content in the body) from a PUT. If using PUT for create, return HTTP status 201 on successful creation. PUT is not safe operation but it’s idempotent. 4. PATCH: The PATCH method used for modify capabilities. The PATCH request only needs to contain the changes to the resource, not the complete resource. This resembles PUT, but the body contains a set of instructions describing how a resource currently residing on the server should be modified to produce a new version. This means that the PATCH body should not just be a modified part of the resource, but in some kind of patch language like JSON Patch or XML Patch. PATCH is neither safe nor idempotent. 5. DELETE: The DELETE method is used to delete a resource identified by a URI. On successful deletion, return HTTP status 200 (OK) along with a response body. Credits: REST API (Introduction) - GeeksforGeeks www.tharinduweerasinghe.com
  • 6.
    www.tharinduweerasinghe.com Sample usage ofthe HTTP Methods in the Requests Credits: https://assertible.com/blog/7-http-methods-every-web-developer-should-know-and-how-to-test-them
  • 7.
    HTTP Status Codes •Credits: https://faun.pub/consuming- rest-apis-with-python- eb86c6b724c5 www.tharinduweerasinghe.com
  • 8.
    Example: Calling a RESTAPI End-Point in Javascript www.tharinduweerasinghe.com
  • 9.
    The following slidewill show you how can you include your JavaScript code shown in the previous slide in an HTML code! www.tharinduweerasinghe.com
  • 10.
  • 11.
    3 steps todesign API end-points using Python • Step 1: Design your API Interface • The most vital step • You need to know the problem you want to address for a successful design • Basically, this will cater the business requirements of sending relevant data for respective HTTP requests • You need to decide on the format of the request and response • Use API URL best practices in designing your APIs. • You need to describe your REST APIs using OpenAPI definition, designing an API means creating an OpenAPI document for it. Credit: https://blog.stoplight.io/how-to-create-an-api-in-three-steps www.tharinduweerasinghe.com
  • 12.
    3 steps todesign API end-points using Python (contd.) • Step 2: Mock your API Server • Once you have your design, you need to check the manipulated response. • After you create your OpenAPI document for your new API, create a mock server. There are a few ways to do this, one of the methods is to use an open-source command-line utility like the Prism API server. www.tharinduweerasinghe.com Credit: https://blog.stoplight.io/how-to-create-an-api-in-three-steps
  • 13.
    3 steps todesign API end-points using Python (contd.) • Step 3: Code your API • Once you have your OpenAPI document then, you can select whatever the platform or language you would like to code your API and build the API server. Here I’m using Python with a library called Flask. www.tharinduweerasinghe.com Credit: https://blog.stoplight.io/how-to-create-an-api-in-three-steps
  • 14.
    REST API TestingTools • Postman • Swagger https://www.softwaretestingmaterial.com/best-api-testing-tools/
  • 15.
    Reference • Understanding AndUsing REST APIs — Smashing Magazine • REST API (Introduction) – GeeksforGeeks • Python and REST APIs: Interacting With Web Services – Real Python • 7 Simple REST Client Examples for Retrieving API Data (dreamfactory.com) • REST API Example Requests – Tableau • Building a Basic RestFul API in Python | Codementor • REST API Testing Strategy: What Exactly Should You Test? l Sisense • Online API Testing Tool | Test Your API Online (reqbin.com) • Postman API Platform | Sign Up for Free • SwaggerHub | API Design and Documentation with OpenAPI • Python REST API Tutorial - Building a Flask REST API – YouTube • https://levelup.gitconnected.com/all-possible-ways-of-making-an-api-call-in-plain-javascript-c0dee3c11b8b • https://www.testim.io/blog/the-9-api-testing-tools-you-cant-live-without-in-2019-2/ • https://www.freecodecamp.org/news/rest-api-best-practices-rest-endpoint-design-examples/ • https://www.freecodecamp.org/news/rest-api-best-practices-rest-endpoint-design-examples/ • https://rapidapi.com/blog/how-to-use-an-api-with-javascript/ • https://www.softwaretestingmaterial.com/best-api-testing-tools/ www.tharinduweerasinghe.com