Introduction to Pre-request Script

PruTAN empowers you to add dynamic behavior to your REST API requests using pre-request scripts. These scripts allow you to write test suites and build requests with dynamic parameters. You can include JavaScript code that executes based on events in the request flow.

  • Pre-request scripts are executed before a request is sent to the server.
  • You can add multiple functions or methods to a script for a request.
  • Pre-request scripts can be added to both saved and unsaved requests in a collection.

PruTAN will then execute the script along with the requests in the specified order.

Utilizing Pre-request Script

A pre-request script is a piece of code that runs before the request is executed. You can use the pre-request script for pre-processing task , such as:

  • Adding/Updating body data
Accessing Application/XML Body

If the body data of your request is in XML format, you can utilize the xmlQuery() method along with the doc keyword to access and modify the XML elements. The xmlQuery() method requires two inputs: the doc object and the path of the XML element you want to update. It is important to note that the doc object should be in lowercase as it is case sensitive.

Here's an example: Let's simulate a XML request in our Hostbox environment which will take a request body & response body as below :

  • To know more about Request simulation follow : Hostbox
// Request Body Data 
<?xml version="1.0" encoding="UTF-8"?>
<orderRequest>
  <customer>
      <customerId>123456</customerId>
      <name>John Doe</name>
      <email>john.doe@email.com</email>
  </customer>
  <products>
      <product>
          <productId>789</productId>
          <productName>Laptop</productName>
          <quantity>2</quantity>
          <price>1000.00</price>
      </product>
  </products>
  <shippingAddress>
      <address>123 Main St</address>
      <city>Anytown</city>
      <zipCode>12345</zipCode>
      <country>USA</country>
  </shippingAddress>
  <paymentMethod>
      <cardNumber>1234-5678-9012-3456</cardNumber>
      <expiryDate>12/25</expiryDate>
      <cvv>123</cvv>
  </paymentMethod>
</orderRequest>


//Response Body Data
<?xml version="1.0" encoding="UTF-8"?>
<orderResponse>
  <orderId>ODR123456789</orderId>
  <status>Success</status>
  <message>Your order has been placed successfully!</message>
  <totalAmount>2000.00</totalAmount>
  <shippingDetails>
      <estimatedDeliveryDate>2024-01-05</estimatedDeliveryDate>
      <shippingMethod>Standard</shippingMethod>
      <shippingCost>50.00</shippingCost>
  </shippingDetails>
</orderResponse>

Now execute this Hostbox request from API module with this below request body and Pre-Request Script :

// Request Body Data 
 <?xml version="1.0" encoding="UTF-8"?>
<orderRequest>
   <customer>
       <customerId>123123</customerId>
       <name>John Doe</name>
       <email>john.doe@email.com</email>
   </customer>
   <products>
       <product>
           <productId>789</productId>
           <productName>Laptop</productName>
           <quantity>2</quantity>
           <price>1000.00</price>
       </product>
   </products>
   <shippingAddress>
       <address>123 Main St</address>
       <city>Anytown</city>
       <zipCode>12345</zipCode>
       <country>USA</country>
   </shippingAddress>
   <paymentMethod>
       <cardNumber>1234-5678-9012-3456</cardNumber>
       <expiryDate>12/25</expiryDate>
       <cvv>123</cvv>
   </paymentMethod>
</orderRequest>

 // Pre-Request Scripts 

   // Update XML value
   xmlUpdate(doc, 'orderRequest.customer.customerId', '123456');

In the above example, the customerID of the request body should be 123456 but it is 123123, but to get the response the customerID should be updated before request execution and it is fullfilled by PruTAN's Pre-request feature by using the script mentioned above and we will get the expected response. User can use similar scripts to modify XML data as needed.

By utilizing pre-request scripts in PruTAN, you can customize and enhance your API requests with dynamic behavior, making your testing and development processes more efficient and effective.

Accessing Application/JSON Body

In PruTAN, if the body data of your request is in JSON format, you can use dot notation (.) with the doc object to access and manipulate the request data before executing the request.

Here is an example of how you can work with JSON body data in the pre-request script:

// Body Data
{
  "Id": 12345,
  "Customer": "John Smith",
  "Quantity": 1,
  "Price": 10.00
}

// Access and modify fields
doc.Price = 50;

Now user can see this updated Price in the body of request payload.

In the example above, the JSON body data is accessed using dot notation (.) with the doc object. You can use dot notation(.) to access specific fields within the JSON structure and update their values accordingly.

By utilizing the pre-request script in PruTAN, you have the flexibility to access and modify JSON body data, allowing you to customize and tailor your requests as needed before they are sent to the server.