Forum Discussion

RobertDAltman's avatar
RobertDAltman
Occasional Contributor
7 years ago
Solved

Parsing REST API result in keyword test

Ok, so now that I've successfully called my REST API from my keyword test, how do I parse the result (presumably JSON, although I can just as easily return XML)?  Do I need to roll my own script code...
  • RobertDAltman's avatar
    7 years ago

    EDIT: Thanks Alex for the guidance re: getting the LastResult.  I've updated my own answer to my question accordingly.

     

    After a bit of fiddling with the product (bear in mind, I'm learning as I go along... I just started using TC a few days ago), here's a sequence of KeywordTest steps that calls the petstore API and parses out a value from the result.

     

    // Call the petstore API.  This returns a JSON string

    SendRequest | "GET", "http://petstore.swagger.io/v2/store/inventory"

     

    // To get the JSON string from the previous call, I need to use a Set Variable action to fetch

    // the magic LastResult (i.e. the JSON string returned by the previous API call)

    Set Variable Value | apiResult [Local] | LastResult

     

    // Now that I have the JSON string, parse it and store the result in a variable I can use later

    // in the Keyword Test.  In this example, I'm parsing the "sold" member.

    Set Variable Value | value [Local] | JSON.parse(KeywordTests.MyTest.Variables.apiResult).sold