Contributions
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 asap1.8KViews0likes2Comments- 936Views0likes0Comments
- 967Views0likes1Comment
Re: Need to update/set Header Values through groovy scripting
Hi rupert, Thanks for replying back again. This is how I need to do ... I first created a Empty Project > Mock Service. Underneath this, created a Mock action 'POST' (resource path being: /rest/login). Underneath this, a JSON mock response added. So when I run the mock service with resource path I get the json mock response directly. Now, in addition to the resource path I am trying to add a request body (also in Json), something like this {"username": "admin", "password": "admin"}. Then, I wrote a script which must alter the Accept Header Field in request body, as well as append the json message to the request body and calculate the Content-Length the request body. import java.lang.* import java.util.* import java.io.* import java.net.* import groovy.lang.* import groovy.util.* import javax.servlet.http.* def groovyUtils=new com.eviware.soapui.support.GroovyUtils(context) import groovy.json.JsonSlurper import com.eviware.soapui.model.iface.* log.info("response="+mockRequest) def query = mockRequest.getRequest() //log.info(query) def ContentType = mockRequest.getRequest().getHeader("Content-Type").toString() //log.info(ContentType) def ContentLength = mockRequest.getRequest().getHeader("Content-Length").toString() //log.info(ContentLength) def Accept = mockRequest.getRequest().getHeader("Accept").toString() //log.info(Accept) def method = mockRequest.getMethod().toString() //log.info(method) def path = mockRequest.getPath().toString() //log.info(path) jsonAsText = '''{ "username": admin, "password": admin, "device-token": AA}''' //log.info(jsonAsText) def RequestBody = method +" " +path + '\n' + "Content-Type: "+ContentType + '\n' + "Accept: "+Accept + '\n' + "Content-Length: "+ContentLength + '\n' + '\n' + jsonAsText log.info(RequestBody) RequestBody.setRequestContent return "JSON" How do I alter the request body before returning the JSON response ? Thanks in advance.7.2KViews0likes1CommentRe: Need to update/set Header Values through groovy scripting
Similar to what you have posted. What you showed me is the response, but what I need is in the request section. I am getting this as request body ... POST rest/login Accept: /* Content-Type: application/json Content-Length: 8 but I need it like this ... POST rest/login Accept:application/json Content-Type: application/json Content-Length: [length of this message] {'username':'admin', 'password': 'admin'} AND POST IT AS REQUEST BODY. WOULD THIS BE POSSIBLE ?7.3KViews0likes3CommentsNeed to update/set Header Values through groovy scripting
Hi, I have created a REST Mock response. To invoke the response; I have the URI and request body. def Accept = mockRequest.getRequest().getHeader("Accept") log.info(Accept) This is giving me /* as response in console, but I need to update it as "application/json" def Method = mockRequest.getRequest() log.info(Method) This is giving me ... POST rest/login Accept: /* Content-Type: application/json Content-Length: 8 ...... But what I want is along with the request body like POST rest/login Accept:application/json Content-Type: application/json Content-Length: [length of this message] {'username':'admin', 'password': 'admin'} How can I update or set the request header and the body through the script ?Solved7.4KViews0likes5CommentsHow to get XML and JSON mockesponse at the same time ?
I have created a rest mock service which has JSON response. While sending out Mock requests, it shows only JSON and it's corresponding XML output is not shown. So I created 2 mock responses; one with XML and other JSON. But everytime I execute the request, it gives mock responses SEQUENTIALLY, first time XML response and second time JSON and happens in loop. So do I need to create a groovy script in order to get mock responses in both XML and JSON formats at the same time ???606Views0likes0Comments