Search code examples
karate

How to get specific value from dynamic json response and use it to the next request


I'm really newbie at this and trying to learn KARATE.

I have a POST request that is not showing the ID on the response. My problem is that I'm gonna do a CRUD(Create, Read, Update, Delete) scenario on the one I created, but I cannot search the ID(which requires from the delete endpoint)

So now I'm not sure how to find that recent ID created since there's a lot of value in the response.

  • The CreateSomething is coming from a json file
  • The response on GET Request has "code", "id", "name", etc

Here's my code

`Scenario: CRUD E2E
    ##POST
    Given path (post endpoint)
    When request CreateSomething
    When method post
    Then status 201
    
    ##GET
    Given path (get endpoint)
    When method get
    Then status 200
    * def UUIDres = UUID
    And match response contains deep {"code": "AUTOMATION", "id": "#string"}
    
    
    ## Get a Something with specific ID
    Given path (get using ID path)+ UUIDres +''
    When method get
    Then status 200`

What I really want is get the ID of this {"code": "AUTOMATION"} and use it on the next request


Solution

  • If the response from the GET request has data, it is easy to "save" them to variables.

    * def id = response.id
    

    And then:

    * path id
    * method delete
    

    Should work. For a full working example, take a look at this. You can cut and paste it into a feature file and see it work, and you can study how it works.