cancel
Showing results for 
Search instead for 
Did you mean: 

Mock REST Services to return JSON response

Highlighted
Occasional Contributor

Mock REST Services to return JSON response

Hi,
I need to send a JSON response from a Mock REST Service for a GET request. I tried to set the response content type as follows in the OnRequestScript tab, still the response content type is rendered as text/plain instead of Json.

mockRunner.returnFile ( mockRequest.getHttpResponse(), new File (path)) // path is the file path of a text file with static Json Response.
def mockResult = new com.eviware.soapui.impl.wsdl.mock.WsdlMockResult ( mockRequest )
mockResult.setResponseContentType("application/json") // This shows error message as dispatch error; missing response
mockResult.setContentType("application/json")
return mockResult

Tried this in AfterRequestScript tab as well, doesn't work.
mockRequest.getHttpResponse().setContentType("application/json")

Any help is highly appreciated.

Thanks,
Gopi.
6 REPLIES 6
Highlighted

Try this:

- in your mock service, go into your mock response
- in the mock response, enter your JSON content to return from the mock service
- on bottom of response editor expand the headers tab and enter:
Header = Content-Type
Value=application/json

that for me returns JSON response with the right header.

Michael Giller
SmartBear Software


Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Highlighted
Occasional Contributor

Thanks for your response.

However I am not able to edit the response content type as it gets set automatically depending on the file type i am sending. If the file type that i am reading the content happens to be a .txt file, the content type gets set as text/plain, if the file happens to be a .xml, the content type gets set as text/xml.
I want to set a Json object in the response dynamically in the Mock REST response and the content type should be application/json as the client code is expecting this type.

I am not able to locate the header part in response editor of mock service either. Did you refer to response editor inside the Request editor?
Highlighted
Occasional Contributor

We had to write the output to response stream by acquiring mockRequest.getHttpResponse().getWriter() to set the required json content-type.
Highlighted

>on bottom of response editor expand the headers tab and enter:

 

I have a similar error and I cannot locate this feature to expand the headers tab.

 

This mocking feature works simply for my project using old-fashioned SOAP XML web services, but this REST stuff seems more complicated.

 

==

SoapUI-5.2.1 Community/free edition

 

Highlighted

Hi,

 

So you just want to set the content type to application/json for a REST mock response? - Did the drop down not work for you:

 

Screen Shot 2016-04-25 at 10.29.24.png

If you need to set a REST mock reponse's content type dynamically, then I have done it with script like:

 

def headers = mockResponse.responseHeaders
headers["Content-Type"]=["application/json"]
mockResponse.responseHeaders=headers

Where the mockResponse variable is available in the response's script tab.

 

The last time I did this was with version 5.0, but I think this functionality has been there a while.

 

Regards,

Rupert

 

Author of SoapUI Cookbook
Highlighted

When you need create the file on run-time.. using attachement is quite impossible. Then you need script. 

 

I just did this simple response script. It will read your file. Just replace file name with your preference. It works with all file format. (or with formats, which I tested )..

 

// get the path to the folder where your project resides.
def projectPath = new File(context.mockService.project.path).parent
// Specify the file path that is relative to the project’s path.
def fileName = projectPath + "\\response\\CABIS-MOCK_NIST1.tdf"
log.info "CABIS-MOCK send response with file: " + fileName

import java.io.FileInputStream
import java.io.File

File f = new File(fileName)
FileInputStream fis = new FileInputStream(f)
byte[] b = new byte[f.length()]

//log.info javax.xml.bind.DatatypeConverter.printHexBinary(b) //returns a hex string
//requestContext.mockResponse.responseContent =  javax.xml.bind.DatatypeConverter.printHexBinary(b)
//log.info f.text
requestContext.mockResponse.responseContent = f.text
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors