5
votes

I am trying to get value of TxnType from json request while mocking a response from SOAPUI. I want to respond with different responses based the value of TxnType.

{
    "Request": {
        "UserId": "user",
        "TxnType": "fetch"
    }
}
2

2 Answers

2
votes

Here is the groovy script to fetch the request value with fixed json

def json = """{
    "Request": {
        "UserId": "user",
        "TxnType": "fetch"
    }
}"""

def transactionType = new groovy.json.JsonSlurper().parseText(json).Request.TxnType
log.info "TxnType is : ${transactionType}"

You may quickly try the Demo

If you want to use the dynamic json in the mock script, then you can use below mock script dispatcher

def transactionType = new groovy.json.JsonSlurper().parseText(mockRequest.requestContent).Request.TxnType
log.info "TxnType is : ${transactionType}"
1
votes

// Pick Request Body

def requestBody = mockRequest.getRequestContent()
log.info "Request body: " + requestBody

// Pick TxnType value

def txnType = requestBody["TxnType"]

(or something like this)