Test REST API with unREST – part 2

In the previous post, we’ve seen the basics how to work with unREST. This time, I’ll showcase some advanced and (hopefully) more interesting features it has.

Sending request body

With HTTP POST requests, you usually need to send some data as request body, usually JSON. unREST provides convenient ways to add this data:

In this example, we specify body content as a value of property content . Please note that quotation marks must be escaped for string values. If you now execute post createPost , unREST will send POST request to specified endpoint and with the request body you set.

This way of setting body content might be OK for simple payloads, but for more complex data, you will probably want to save content in a file, and reference it from test case. This is possible with unREST. Another way of writing the previous  test case would be like this:

Here, instead of specifying content directly in test case, we reference a file which contains the body. We will also need to create a file called post_data.json  in current directory:

Now, you can run post createPostFromFile  in unREST console, and you should get the same result as in previous case.

Built-in commands

unREST contains some useful commands that can help in manipulating request and response data. In this section, we will see these commands and their use cases.

Extract data from response

Command extract  is used to extract parts of the request, such as specific headers, body or response status. This command is usually used piped to response output. For example, to extract body of the response, you could use the following command:

This will print out response body of the specified request. You could also extract specific headers of the response:

This command can be useful for extracting and saving specific response values, such as authentication tokens. This is shown in the next section.

Saving values

Command save  is used to save ke-value pairs, ie. similar to setting environment variables. For example, you could set the following variable:

This will create variable var  and assign to it value value . After this, this variable can be accessed  and used in test cases. As a sample use case, consider the following snippet:

This will extract Content-Type  header from the response and save it’s value as variable type .

In these eamples, we used option -s to save variable values in the current session. It is also possible to save these values to file by providing option -f.


Display environment data

Command env  displays current configuration settings and variable values.

As you can see, env  displays all configuration settings, test cases and variables we saved so far.

Final notes

This is about everything available so far. I hope somebody finds this interesting and useful in some situations.

Code for this project is freely available in Github under MIT license, so if anybody wants to take a shot at hacking it and adding some new features, please feel free to do do. Also, code for the samples in this post is also available here.

Leave a Reply

Your email address will not be published. Required fields are marked *