Forum Discussion

bkbn16's avatar
bkbn16
Contributor
8 years ago

How to send a pretty json request in a mockrequest body ?

I need to send a POST message body in JSON along with the URL to fetch a JSON response. Hence, I created a Empty roject > New MockRest Service > Add new Mock Action. Under this, created POST method with resource path "rest/login".Also added a JSON response under the Mock Action. 

 

On the MockAction, selected 'SCRIPT'. 

 

import groovy.json.*

def json = new JsonBuilder()

//json username: "admin", password: "admin", app_id : "PR"
//log.info json

def output = JsonOutput.toJson([username: "", password: "", app_id: "PR"])
log.info output

// Match based on body
def requestBody = mockRequest.getRequestContent()
log.info "Request body: " + requestBody

if( requestBody.contains(output) )
{
// return the name of the response you want to dispatch
return "JSON"
}

 

The output is: Mon Jul 25 13:20:27 IST 2016:INFO:{"username":"","password":"","app_id":"PR"}

 

When I send URL and POST message body in pretty format of JSON message, no response is seen. If I send the JSON in {"username":"","password":"","app_id":"PR"} this format; the response is coming fine. 

 

 

Is the output in text or json format ? How should I send a json message to get the response ? PFA both screenshots. I am confused that the output variable is giving out String or Json value !!! Please comment asap

2 Replies

  • And please don't suggest me to go for test suite or test cases, I need to do this in mock action itself