Create a script for a REST API

In recent years, Representational State Transfer (REST) has become a popular model for software architecture, especially for web-based applications. Most large websites, for example Twitter, Google, or Flickr, use REST APIs. Using the web_rest function, you can create a load test script for a REST API.

REST API components

A REST API call consists of the following components:

Uniform Resource Identifier (URI)

A string comprised of the host, the path of the functional component, and the Query string, with key-value pairs. For example :

http://www.shopping.mydomain.com/en_US/home-office/-/products/Tablets/Tablets?SearchParameter=ElitePad

Method

The action to perform, such as GET, POST, PUT, and DELETE.

Data

The data to send to the server, usually in JSON format.

All of these components are included as parameters of a web_rest function. For details, see the Function Reference (select the relevant version).

Back to top

Generate web rest functions

To generate web_rest functions, do one of the following:

  • Select Design > Insert in Script > REST API
  • Right-click in the Script Editor and select Insert > REST API

The REST Step Properties dialog box that opens includes various tabs that help you to generate and check the required web_rest functions.

Tab Function
Build Step

Define the web-rest step:

  • Specify the required URL and select a method.

    The request body editor area changes slightly depending on whether the selected method can have a body attached to it or not.

  • Specify the required URL parameter key-value pairs. As you add key-value pairs, VuGen adds them to the URL above.

    Click +Add to add additional key-value pairs.

  • Specify the required parameters for the headers. You can enter any string as the header name, or you can select one of the common headers from the drop down list.

    Click +Add to add additional header parameters.

  • Specify the required values for the body parameters. Click +Add to add additional body parameters.
Preview Shows a preview of the request code that is generated, based on the data that is contained in the Build Step tab. This is the code that is sent to the server when the resulting web_rest function is run.
Step Results

Shows the HTTP response that was returned by the server after the most recent step run. The response is divided between the Body, Cookies, and Headers tabs.

The status of the response appears in the top right corner of the dialog box.

  • After you have entered the required attributes for the web_rest function, you can click Run Step to run the step to test that it functions correctly. The HTTP response to the step run is displayed in the Step Results tab. The HTTP status of the test run appears in the top right corner of the dialog box.
  • After you have entered and checked the required attributes of the step, click Insert Step to insert the web_rest function into your script, at the cursor location.

Note: After you click Insert Step to insert the step into your script, VuGen adds the step to the step history list. Click Step History (located on the left of the REST Step Properties dialog box) to show the step history list.

You can select any step in the list to insert the step details into the Build Step tab. You can then modify the step, run it, and insert it into the script.

A separate step history list is maintained for each computer on which VuGen is installed. If required, click Clear History to clear the step history list.

Back to top

Generate a web.rest function in JavaScript

To generate a web.rest function in JavaScript :

1. Record your script in JavaScript. See Record your Vuser script in JavaScript.

2. Press Ctrl+Shift+W to launch the REST Step Properties dialog box and define the web.rest function. See Generate web rest functions above.

Back to top

Edit an existing web_rest function

To open the REST Step Properties dialog box to edit an existing web_rest function, right-click inside the function and select Show Arguments.

Back to top

Create script steps for a Web - HTTP/HTML script that calls a REST API

  1. Create a Web (HTTP/HTML ) Vuser script. For details see, Create and open Vuser scripts.

  2. Record the REST API application while you perform typical business processes in the application.

    After you finish recording, add new web_rest calls using the following format, where the attributes are URL, Method, ResType, Body, and BodyFilePath:

    web_rest("<request_name>",
    "<Attribute_List>",
    LAST);
  3. (Optional) To reference a file with the JSON data instead of entering the actual text:

    1. In the Solution Explorer, right-click the Extra Files node and select Add Files to Script to add the .json data file.

    2. Replace the Body argument with BodyFilePath=<file_name.json>.

    3. Allow JSON files. Select Tools > Options > Scripting > Script Management and add .json to the Allowed Extensions list.

Examples

The following example shows a REST API function that updates values using a PUT action:

web_rest("update customer info",
"URL=http://myServer/customers/{cust_id}",
"Method=PUT",
"ResType=JSON",
"Body={ \"address\":\"yyyy\", … }",
LAST);


The next example shows a REST API function that uses data from a file when updating values using a POST action:

web_rest("create new customer",
"URL=http://myServer/customers/",
"Method=POST",
"ResType=JSON",
"BodyFilePath=c:\\my_data\\data.json", /* BodyFile for large content */
LAST);

Back to top