Forum Discussion

ar1607's avatar
ar1607
New Contributor
8 years ago
Solved

[SoapUI 5.2.1] mockRequest is NULL in REST MockService

I'm using SoapUI 5.2.1 and I have a REST MockService. Whenever I try to implement the following script, I get the NullPointerException .

 

Script:

def requestBody = mockRequest.getRequestContent()
log.info "Request body: " + requestBody

 

Error:

com.eviware.soapui.impl.wsdl.mock.DispatchException: Failed to dispatch using script; java.lang.NullPointerException: Cannot invoke method getRequestContent() on null object

 

This is a very frustating error that I'm continuously getting, and surprisingly there is no helpful solution anywhere. Please Help!!!!

 

 

PS : I'm trying to get the content of a HTTP GET request.

  • Hi,

     

    Like Rao said, we need a few more details, the functionality should definitely work. Here is my mock setup for reference:

    The output you can see in the (script) log is the result of me making a GET request of: http://localhost:9090/test?p=hello

     

    Regards,

    Rup

     

14 Replies

  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    Hi,

     

    You can certainly get the request body in using the code you have for REST POST requests.

     

    But for GET requests the request body is null, why are you trying to get the request body? Do you actually want the GET request parameters?

     

    Sorry if I've misunderstood what you are trying to do.

     

    Regards,

    Rupert

    • ar1607's avatar
      ar1607
      New Contributor

      Hi,

       

      Yes, I'm trying to get the GET request parameters.

      • rupert_anderson's avatar
        rupert_anderson
        Valued Contributor

        Ok, no problem. 

         

        When you create the mock response, in the script tab there are usually a variety of examples - did you see anything like:

         

        // Script dispatcher is used to select a response based on the incoming request.
        // Here are few examples showing how to match based on path, query param, header and body
        
        // Match based on path
        def requestPath = mockRequest.getPath()
        log.info "Path: "+ requestPath
        
        if( requestPath.contains("json") )
        {
            // return the name of the response you want to dispatch
            return "JSON Response"
        }
        
        
        // Match based on query parameter
        def queryString = mockRequest.getRequest().getQueryString()
        log.info "QueryString: " + queryString
        
        if( queryString.contains("stockholm") )
        {
            // return the name of the response you want to dispatch
            return "Response Stockholm"
        }
        else if( queryString.contains("london") )
        {
            // return the name of the response you want to dispatch
            return "Response London"
        }
        
        
        // Match based on header
        def acceptEncodingHeaderList = mockRequest.getRequestHeaders().get("Accept-Encoding")
        log.info "AcceptEncoding Header List: " + acceptEncodingHeaderList
        
        if( acceptEncodingHeaderList.contains("gzip,deflate") )
        {
            // return the name of the response you want to dispatch
            return "GZiped Response"
        }

        In particular:

         

        def queryString = mockRequest.getRequest().getQueryString()
        log.info "QueryString: " + queryString

        This should get you the query string parameters for your GET request.

         

        Regards,

        Rup

         

  • kanchanruia's avatar
    kanchanruia
    New Contributor

    I am getting the same error. How were you able to solve this?


    ar1607 wrote:

    I'm using SoapUI 5.2.1 and I have a REST MockService. Whenever I try to implement the following script, I get the NullPointerException .

     

    Script:

    def requestBody = mockRequest.getRequestContent()
    log.info "Request body: " + requestBody

     

    Error:

    com.eviware.soapui.impl.wsdl.mock.DispatchException: Failed to dispatch using script; java.lang.NullPointerException: Cannot invoke method getRequestContent() on null object

     

    This is a very frustating error that I'm continuously getting, and surprisingly there is no helpful solution anywhere. Please Help!!!!

     

     

    PS : I'm trying to get the content of a HTTP GET request.


     

    • jozefg's avatar
      jozefg
      Visitor

      Hello.

      I'm getting probably the same issue in POST requests. I'm trying to read JSON request body in the dispatch script of my RESTfull mock in SOAPui.

       

      // log the request body
      log.info(mockRequest.getRequestContent())

       

      // Parse the JSON request.
      def requestBody = new groovy.json.JsonSlurper().parseText(mockRequest.getRequestContent())

       

      // Set up MSISDN value from the request message.
      requestContext.msisdn = requestBody.phone_number
      requestContext.uuid = requestBody.MC_UUID

      return "Response_OK"

       

       

      After mock restart it works just once. When I repeat the request with same (or different) values it stops on Null value in getRequestContent.

       

      I'm using SOAP UI 5.5.0 for mock and Postman 7.16 for client.

       

      Sending the request

       

      POST http://localhost:8181/v1/identity

      {
      "MC_UUID":"550e8400-e29b-41d4-a716-446655440000",
      "phone_number":"31657100013"

      }

       

      with headers

      Content-Type:application/json

      Accept:application/json

      User-Agent:PostmanRuntime/7.16.3

      Content-Length:89

      Connection:keep-alive

      Host:localhost:8181

      Cache-Control:no-cache

      Accept-Encoding:gzip, deflate

       

      In the script log of the SOAP ui mock I can see

      Mon Sep 09 18:14:40 CEST 2019:INFO:{"MC_UUID":"550e8400-e29b-41d4-a716-446655440000","phone_number":"31657100013"}

       

      Mon Sep 09 18:14:46 CEST 2019:INFO:

       

      So 1st time the request passed with correct body, 2nd time the same POST is delivered with empty body.

      I need always to restart the mock to make the 1st test working.

      Very strange behaviour.

      Thanks in advance for any hints & advices.

      Jozef