Blogs | Srijan

How to Send Dynamic Auth Request with Postman in Drupal REST

Written by Shamsher Alam | Sep 11, 2017 7:00:00 AM

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:

  • 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.

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.

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.

Edit response method and format after request

Step 3: Send Authenticated Request

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

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)

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

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).

JSON value format for sending node values with web request

Drupal node create with author information.

User logout and destroy Drupal session.

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.