RESTful is a wonderful little module that provides a solid object oriented base for implementing a REST API in Drupal. I have used both RESTful and services to create APIs and at the moment I prefer to work in RESTful – though the class based PHPoop approach is really vital to both. The maintainers are conscientious, in-tune with API best practices and write quality code. I encourage you to contribute to their project by reporting issues, contributing code in pull requests in GitHub and most importantly using this great module.

However, there are two issues that I have always faced with REST:

  • How do I send auth request in Drupal REST api?
  • How do I send user session with every authenticated web request in Postman tool?

Now, there are two ways to resolve these issues:

  • One is, we can use the drupal_http_request function to consume the data and use it. But that would require a lot of customizations and in the long run would be very difficult to maintain.
  • Alternatively, we can use the Postman tool auth request.

In this blog, I tried to put together the steps for the second solution. Here is how you can send dynamic session request for next authenticated request using the Postman test script:

Before we start, enable the following modules:

Image 1.png

  • Web Service - Handle all web service core and custom
  • Web Service Authentication – It enables to add the web service authentication request
  • Web Service Server – REST Server or XML-RPC server parser

Download Google Chrome Postman extension application.

For this purpose, I have used Drupal 7 and the Services module. The first step would be to download and install the Services module and enable the REST Server module that comes with it.

Step 1: Add service

To create a service, you have to navigate to Service UI (admin/structure/services), click "add service", and enable session authentication for a user request.

new_end_point.png

Step 2: Edit the Resources Page

The "Open Resources" page will show Drupal core default service list like node create, user, taxonomies and file creation. You can also create your own custom web services using service module API.

Now edit resources.

image 3.png

Resources are available in default setup where all resources come with the Services module. You can choose or enable any default services end point for accessing web request.

image 4.png

Edit response method and format after request

image 5.png

Step 3: Send Authenticated Request

Now time to check web services and send the authenticated request. (api/user/login)

image 6.png

Store user session value in a variable using Postman script under tests tab. Write Postman tests script before sending request because when you will send web request, so post script will run when you will get data and save session value from response. (See screenshot for reference)

Image 7.png

This is the code example of Postman script before sending web request, and store session value from the response.

image 8.png

Node creates or next web authenticated request from Postman and uses user session values. Follow these steps to send next authenticated request (see screenshot for reference).

image 9.png

JSON value format for sending node values with web request

image 10.png

Drupal node create with author information.

image 11.png

User logout and destroy Drupal session.

image 12.png

You can download sample Postman request from GitHub

If you found this helpful, you might want to check out our range of tech webinars as well.