Skip to content

Response capture

Many times we want to reuse in other requests a value obtained in a response, like:

  • An HTTP call receives an authorization token and this token must be used in other requests.
  • A response contains ids that must be passed in other calls.

We can do that on Pororoca with captures.

ResponseCaptures

A capture takes the value of an HTTP response and saves this value in a variable of the current environment. If no environment is active, then the variable will be saved into the collection variables.

If it's not possible to capture a value in a response, the variable's value remains unchanged.

Header captures

They are based on the name of the header whose value will be captured.

JSON body capture

It is applied when the response has a JSON Content-Type.

The capture syntax is similar to other languages: $ means root (start object or array), .myProp means accessed field, [0] means 1-th item of the accessed array.

Consider the following JSON:

json
{
    "id": 18,
    "name": "Miyagi",
    "items": [
        {
            "objectId": 1,
            "product": "Laranja"
        },
        {
            "objectId": 2,
            "product": "Acerola"
        }
    ]
}

Some examples of captures and values below:

Body pathValue
$.id18
$.nameMiyagi
$.items.count()2
$.items[0]{ "objectId": 1, "product": "Laranja" }
$.items[0].objectId1
$.items[0].productLaranja
$.items[1].objectId2
$.items[1].productAcerola

If the root object is an array, the indexation also applies:

json
[
    {
        "id" : 186
    },
    {
        "id" : 244
    }
]
Body pathValue
$.count()2
$[0].id186
$[1].id244
$.first().id186
$.last().id244
$[0]{ "id": 186 }
$[1]{ "id": 244 }

XML body capture

It is applied when the response has a XML Content-Type.

The syntax used is XPath 1.0.

Consider the following XML:

xml
<A>
  <B>qwerty</B>
  <C>
    <D>uiop</D>
  </C>
</A>

Some examples of captures and values below:

Body pathValue
/A/Bqwerty
/A/C/Duiop

Example of a XML with namespaces and prefixed nodes:

xml
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing">
    <env:Body>
        <xsi:response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <wsa:MyVal1>Roraima</wsa:MyVal1>
            <xsi:Value>
                <wsa:MyVal2>123987456</wsa:MyVal2>
            </xsi:Value>
        </xsi:response>
    </env:Body>
</env:Envelope>

If the XML has nodes with namespace prefixes, the capture might be a bit more complicated and in some cases, the XPath function local-name() may be used to ignore prefixes.

Examples of captures and values below:

Body pathValue
/env:Envelope/env:Body/xsi:response/wsa:MyVal1Roraima
/env:Envelope/env:Body/xsi:response/xsi:Value/wsa:MyVal2123987456
/*[local-name()='Envelope']/env:Body/xsi:response/wsa:MyVal1Roraima

The online tool xpather.com can aid you to analyse and debug XPath captures.