Forum Discussion

gopiharan79's avatar
gopiharan79
Occasional Contributor
11 years ago

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

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    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
  • gopiharan79's avatar
    gopiharan79
    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?
    • jamescollett's avatar
      jamescollett
      Contributor

      >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

       

      • rupert_anderson's avatar
        rupert_anderson
        Valued Contributor

        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:

         

        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

         

  • gopiharan79's avatar
    gopiharan79
    Occasional Contributor
    We had to write the output to response stream by acquiring mockRequest.getHttpResponse().getWriter() to set the required json content-type.