Drupal/C4/RESTful-API-with-a-REST-Client/English

From Script | Spoken-Tutorial
Revision as of 10:58, 8 June 2018 by Priyacst (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual cue
Narration
Slide 1:

RESTful API with a REST Client

Welcome to the spoken tutorial on RESTful API with a REST Client.
Slide 2:Learning Objectives
  • What is REST client
  • Retrieve data - GET method
  • Create a new node - POST method
  • Modify an existing node - PATCH method
  • Delete an existing node - DELETE method


In this tutorial, we will learn about REST client.


We also learn to use the REST client to

  • Retrieve data using GET method
  • Create a new node using POST method
  • Modify an existing node using PATCH method and
  • Delete an existing node using DELETE method


Slide 3:

System requirement

To record this tutorial, I am using

  • Ubuntu Linux 16.04 OS
  • Drupal 8
  • Postman as a REST Client
  • Firefox web browser


To record this tutorial, I am using
  • Ubuntu Linux 16.04
  • Drupal 8
  • Postman as a REST client and
  • Firefox web browser

You can use any REST client and web browser as per your choice.

Slide 4:

Pre-requisites

  • You should have basic knowledge of Drupal
  • RESTful API implemented in your Drupal website
  • If not, for relevant Drupal tutorials please visit http://spoken-tutorial.org
  • A working Internet connection


To practise this tutorial, you should have basic knowledge of Drupal.


The RESTful API should have been implemented in your Drupal website.


If not, please go through the previous Drupal tutorials on this website.


You should also have a working Internet connection.

Open Drupa8 site First we will confirm whether RESTful API is implemented in our Drupal8 site.


To do so, open our Drupa8 site.

Go to Structure and Views Then go to Structure and Views.


Here you can see that RESTful API is implemented in the Events content type.

Show text Postman Client Note that we have already learnt to implement the RESTful API earlier.


Now we will learnt to check our RESTful API using a REST client.


I will use Postman as a REST client.


You can use any REST client as per your choice.

Slide 6: Postman Client
  • It is a powerful HTTP client for testing web services
  • For installing Postman client, see Additional reading material link


Postman is a powerful HTTP client for testing web services.

To install the Postman client, refer to the Additional reading material link of this tutorial.

Show text Bitnami Drupal Stack


The following steps are applicable to Bitnami Drupal Stack.


But most of the steps are applicable to any other Drupal installation, as well.

Open Postman Client Let us open the Postman client.
Choose GET First we will learn to retrieve data for Anonymous users using GET method.


Choose GET from the drop-down list.

Type http://localhost/drupal/events


Highlight localhost:8080


Highlight drupal


Highlight events

Recall that earlier we had implemented the RESTful API on our Events content type.


We will now enter the path of our RESTful API.


Here localhost:8080 is my server name.


If you are not using Bitnami Drupal stack, please use localhost instead of localhost:8080.


drupal is the folder of my Drupal instance.


events is the content type on which we have configured the RESTful API.

Click Send


Point to the result

Now click on the Send button at the top right corner.


You can see here the contents of our events content type in json format.

Click plus button


Type the path

Next let us learn to retrieve a single node.


Click on the plus button at the top panel to add a new tab.


Choose GET from the dropdown list.


We have to give the exact path of the particular node.

Click Send


Point to the result


Click on the Send button.


You can see here the content of that particular node is retrieved.


<<PAUSE>>

Click plus button


Choose POST

Next let us learn to create a new node for Authenticated users using POST method.


Click on the plus button at the top panel to add a new tab.

Choose POST from the drop-down list.

Type http://localhost:8080/drupal/entity/node?_format=hal+json

Highlight localhost:8080


Highlight drupal


Highlight /node?_format=hal+json

Type the path as shown.


Here localhost:8080 is my server name.


drupal is the folder of my Drupal instance.


We have to hard-code the rest of the things.

Change Type as Basic Auth Under Authorization, we will change the Type as Basic Authentication.
Type username and password Now we will give the username and password of our Drupal website.
Click Preview Request Click on the Preview Request button at the left hand side to update our request.
Click Headers tab


Point to Authentication

Click on the Headers tab.


Here you can see the token for the basic authentication.

Type Content-Type as application/hal+json We will set the Content-Type to application/hal+json.
Click Body tab Now we will go to the Body tab.
Choose raw Let us change the data format as raw.
{

"_links": {

"type": {

"href": "http://localhost:8080:8080/drupal/rest/type/node/events"

}


},

"title":[

{

"value": "Created by an authenticated user!!!"

}

],


"body": [

{

"value": " This is an event!!!"

}
]


}

Here we must declare the required title and type fields.


So type the following code.


This json code is used to create a new node of the type events.


For that we specify the values for title and body fields.


The same code is given in the Code files link of this tutorial.


Please download and use it.

Click Send Click on the Send button at the top right corner.
Point to the json code If the content is successfully posted, you can see the json code of this content at the bottom.
Switch to Drupal site


Point to the first event

We can also check the content in our Drupal website.


Switch back to our Drupal site.


Here you can see the event which is posted from the Postman client.


Note that the UID of this post is 100.


We will use this UID in the postman client to modify this node.


<<PAUSE>>

Switch to Postman Client


Click plus button

Next let us learn to modify a node for Authenticated users using PATCH method.


Let us switch back to the Postman client.


Click on the plus button at the top panel to add a new tab.

Choose PATCH Choose PATCH from the drop-down list.
Type http://localhost:8080/drupal/node/100


Highlight 100

We will enter the URL of the node which we want to modify.


For example, we will modify the node which we created using POST method.


The UID of this node is 100.


We will enter the URL of the node 100.

Change Type as Basic Auth Under Authorization, we will change the Type as Basic Authentication.
Type username and password Now we will give the username and password of our Drupal website.
Click Preview Request Click on the Preview Request button at the left hand side.
Click Headers tab


Point to Authentication

Click on the Headers tab.


Here you can see the token for the basic authentication.

Type Content-Type as application/hal+json We will set the Content-Type to application/hal+json.
Click Body tab Now we will go to the Body tab.
Choose raw Change the data format as raw.
Type

{

"_links": {

"type": {

"href": "http://localhost:8080/drupal/rest/type/node/events"

}


},

"title":[

{

"value": "Changed by PATCH!!!"

}

],


"body": [

{

"value": " The value is changed by patch method!!!"

}
]


}

And type the following code here.


In this code we modify the values for both the title and the body fields.


The same code is given in the Code files link of this tutorial.


Please download and use it.

Click Send Then click on the Send button at the top right corner.
Point to Status OK You can see the Status as 200 OK which means the content is modified.
Switch to Drupal site


Refresh the page


Point to the first event

Let us check the content in our Drupal website.


Switch back to our Drupal website.


Let us refresh the page.


Here you can see that the event is successfully modified using the PATCH method.


<<PAUSE>>

Choose node 8


Point to node 8 in the URL

Next let us learn to delete an existing node for Authenticated users using DELETE method.


I will choose node 100 which was created by me as an authenticated user.


Remember that we have permitted the authenticated users to delete their own content only.

Switch to Postman Client


Click plus button

Switch back to the Postman client.


Click on the plus button to add a new tab.

Choose DELETE Choose DELETE from the drop-down list.
Type http://localhost:8080/drupal/node/8 We will enter the URL of the node which we want to delete.
Change Type as Basic Auth Under Authorization, we will change the Type as Basic Authentication.
Type username and password Now we will give the username and password of our Drupal website.
Click Preview Request Click on the Preview Request button at the left hand side.
Click Headers tab Then click on the Headers tab.
Type Content-Type as application/hal+json We will set the Content-Type to application/hal+json.
Click Send Click on the Send button at the top right corner.
Point to Status 204 No content You can see the Status as 204 No content which means the content is deleted.
Switch to Drupal site Switch back to our Drupal site.
Refresh the page Let us refresh the page.
Point to Page not found We can see that the node is successfully deleted.


<<PAUSE>>

This is all about the managing content through Postman client.


With this, we come to the end of this tutorial.

Slide 7:

Summary

In this tutorial, we have learnt about-

  • Postman client as a REST client
  • Managing content through Postman client using GET, POST, PATCH and DELETE methods


Let us summarize.


In this tutorial, we have learnt about-

  • Postman client as a REST client and
  • Managing content through Postman client using GET, POST, PATCH and DELETE methods


Slide 8:

Assignment

  • Create some new articles through REST Client
  • Modify the articles using PATCH method


As an assignment
  • Create some new articles through REST client and
  • Modify the articles using PATCH method


Slide 9:

Acknowledgement


The video at the following link summarises the Spoken Tutorial project.


Please download and watch it.

Slide 10:

Spoken Tutorial Workshops


The Spoken Tutorial Project Team conducts workshops and gives certificates to those who pass online tests.


For more details, please write to us.

Slide 11:

Acknowledgement


Spoken Tutorial Project is funded by
  • NMEICT, Ministry of Human Resource Development and
  • NVLI, Ministry of Culture

Government of India.

This tutorial is contributed by Vishal Jindal.


This is Priya from IIT Bombay signing off. Thanks for joining.

Contributors and Content Editors

Nancyvarkey, Priyacst