cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to get Rest Response in JSON format using Groovy

SOLVED
Occasional Contributor

Unable to get Rest Response in JSON format using Groovy

Hi All,

 

I have created successfully a test suite with Rest API and I have created groovy script to save request,response in the Local.

Problem what I am facing is I am not getting Rest Response is JSON format in the Local. But I get Request in JSON format.

 

Please refer the attachments for better understanding.

 

Apprecaite if anyone can help me in resolving this.

 

.

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Occasional Contributor

Re: Unable to get Rest Response in JSON format using Groovy

Hi JHunt,

 

I have implemented by adding a jar file(groovy-json-2.1.1.jar) and to add syntax def JsonResponse = JsonOutput.prettyPrint(response)using groovy.

  I achieved the result as expected

6 REPLIES 6
Community Hero

Re: Unable to get Rest Response in JSON format using Groovy

However you are getting your response as a String, you can get the human readable JSON by prettyprinting it:

response.with(groovy.json.JsonOutput.&prettyPrint)

Then write that version to the File.

 

Occasional Contributor

Re: Unable to get Rest Response in JSON format using Groovy

Hi JHunt,

 

   Thanks for your quick reply.I have implemented pretty print statement in my coding.And I cannot find any response JSON format.

Could you suggest someother solution to resolve the issue.

Please find the attachment.

 

 

Community Hero

Re: Unable to get Rest Response in JSON format using Groovy

Can you post your code? Then I can show you where to apply the changes.

Occasional Contributor

Re: Unable to get Rest Response in JSON format using Groovy

Hi JHunt,

 

Sure.I have attached the code for your reference.

Community Hero

Re: Unable to get Rest Response in JSON format using Groovy

 

    response.with(groovy.json.JsonOutput.&prettyPrint)
    res.write(response,"UTF-8")

Change this like this:

 

 

    response = response.with(groovy.json.JsonOutput.&prettyPrint)
res.write(response,"UTF-8")

 

Highlighted
Occasional Contributor

Re: Unable to get Rest Response in JSON format using Groovy

Hi JHunt,

 

I have implemented by adding a jar file(groovy-json-2.1.1.jar) and to add syntax def JsonResponse = JsonOutput.prettyPrint(response)using groovy.

  I achieved the result as expected

New Here?
Join us and watch the welcome video:
Watch the New Interview
7Mistakes
Join the exciting event
SeptemberHubBub
Top Kudoed Authors
Join the September Hub-bub to show off, learn and win