Simple examples on Restful APIs for beginners

phptutorialtutorial

Written by:

Reading Time: 2 minutes

Hi there my good friend,
If you read this post and you found it helpful then that is great. I would want to continue this education by using examples. Sometimes, looking at code snippets can go a long away to aiding your understanding of a subject.
That is why, I would like to follow it up with the following to aid in your development.
I would demonstrate the following:
• Fetching all resources without any authorization
• Fetching a single resource without any authorization.
• Updating a single resource without any authorization.
• Inserting a single resource.
• Forms of authorization
By the time we exhaust these you should be in a better place to write a few lines of code yourself.
Read and absorb it all. All the source code is available on GitHub.

Fetching all resources
We have spoken on the differences between URLs and resources. Thus I would not do much talking on this.

What do we want to do?
It means we want to retrieve all users. This is straight forward.

The request:
Method: GET
URL: http://agbenu/index.php
Code:

Fetching a single resource
What do we want to do?
We would want to retrieve a single record. This is also really straight forward.
Request:
Method: GET
URL:
http://agbenu.com/single.php?id=1
Code:

Updating a single resource
What do we want to do?
We would want to retrieve a single record. This is really straight forward.
Request:
Method: POST
URL: 
http://agbenu.com/update.php
Parameters:
You might be asking yourself why parameters are needed. That is because the information needs to be passed in some way. Since it cannot be passed in the URL then why not pass it within the body of the request.
Code:

Inserting a single resource
What do we want to do?
We would want to retrieve a single record. This is really straight forward.
Request:
Method: POST
URL:
http://agbenu.com/insert.php
Parameters:
All the details needed to insert a new record
Code:

Forms of authorization
All the prior examples have used no form of authorization. However, in this very paranoid world, you need to have at least one means of securing your data from the outside.
You could include any of the following approaches to securing your data;
• Whitelisting IPs
• Including a username and password as part of the post parameters
• Basic authentication as part of the header
• Bearer tokens as part of the authorization header
• OAuth

There are many more innovative ways to place authorization on top of your APIs. These are just a few to get you interested.
All too soon, that is a wrap. Hope these examples were helpful to someone out there. Now go out there, sit in front of your computer and send a few requests.

Share comments, or re-share it for someone out there looking for a simple examples. Let us know how this explanation went down with you. As always, keep learning and cheers.

2 Replies to “Simple examples on Restful APIs for beginners”

  1. Herman says:

    Kindly assist with a blog post on API’s using MSSQL connection.

Leave a Reply

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

Scroll Up