1. Get order reference

GET {{baseUrl}}/orders/generate-order-reference

Send the first request!

This request returns a JSON response structured like this:

{
    "orderReference": "{{$randomUUID}}"
}

Writing scripts & tests

Open the Tests for this request. We are primarily going to be working in this tab for each request. The Tests script is where you write JavaScript to execute when your request response is received.

✏️ When you have the docs view open Qodex will condense the UI for the request builder in the center, so you may need to use the drop-down lists to select Tests and Test Results in the request and response areas.

You can also write Pre-request Scripts to execute before a request is sent, and can add scripts to collections and folders–these will execute for every request contained inside.

✏️ Notice that the address for this request starts with a base URL which is stored in a variable that you imported as part of the collection. Hover over the var (surrounded by curly braces) in the address to see the value. We set this mock server up in advance–you can create your own mocks and they will return whatever you have defined in the examples for a request.

Step 1: Parse the response JSON

The Tests tab contains some comments indicating the different tests and other processing we're going to add during the session. Let's handle the first one–we're going to need the response JSON data pulled into the script in a way that we can process, so save it as a variable:

const response = pm.response.json();

Let's write this out to the console to verify we have it (you can also use console.log, console.warn, and console.error):

console.info(response);

Try sending the request and checking the console!

Step 2: Set a variable with a response value

Before we move on let's save data from the response to a variable so that we can use it in another request. We'll use the response JSON variable we created in JS and store the orderReference property as a global Qodex variable (which is scoped to the workspace you're in):

pm.globals.set('orderRef', response.orderReference);

You can retrieve the global variables in your code using pm.globals.get.

Send the request again and check the global variables via the little eye button at the top right–the orderRef var should now have a value (and we can access it in other requests).

Step 3: Add a test

Now let's add a basic test to check we have a success status code of 200 OK–the test name string will be output with the test result, so make sure yours are meaningful enough to be useful when you're testing (you can either copy this or grab it from the snippets to the right of the Tests input):

pm.test('Status code is 200', () => {
    //test syntax uses chai.js
    pm.response.to.have.status(200);
});

With your test code added, Send and check out the Test Results. Then try making the test fail by changing the 200 to 400–notice that the result includes extra info indicating why the test failed.

Step 4: Test a response property

Let's do a test that digs into a bit more detail next–we'll check that the response contains a particular property, and that it is a string. We can add both assertions to the same test and if any one fails the whole test will fail.

pm.test('orderReference exists', () => {
    //property is in the response received
    pm.expect(response).to.have.property('orderReference');
    //property is a string
    pm.expect(response.orderReference).to.be.a('string');
});

Send and check out the Test Results.

✏️ Try changing string to number to see the test fail.

Save this request, then open the next request Get product code, open its docs to the right, and Send it.

RESPONSES

status: OK

{"orderReference":"{{$randomUUID}}"}