Ask a Question

How to capture values from JSON request and pass to response

viswam_i
New Contributor

How to capture values from JSON request and pass to response

All,

I am working on REST service mocking. As part of this need to capture JSON request and from there capture few values  and pass to response. Can anyone please help me how to do this ?

 

Thanks

Vis

2 REPLIES 2
LoundesM
Contributor

Hi

 

have a look at this http://docs.groovy-lang.org/latest/html/gapi/groovy/json/JsonSlurper.html

 

you will probably use something like this to get your json payload

 

def slurper = new JsonSlurper()
def result = slurper.parseText(mockRequest.getRequestContent())

 

 

use an example from the link above to see how to get specific values

 

within your dispatch script set a property value to hold the respective json element value

 

context.setProperty( "PropertyName", JSONElementValue)

then within your response simply use ${PropertyName} where you want your inbound value to be returned

i.e.

{ "Request value" : "${PropertyName}" }

obviously using a naming convention that suits

 

hope that helps

 

 

nmrao
Champion Level 2

It would be good if you can show the data that you are working with and what data do you need to extract.


Regards,
Rao.
cancel
Showing results for 
Search instead for 
Did you mean: