cancel
Showing results for 
Search instead for 
Did you mean: 

[SoapUI 5.2.1] mockRequest is NULL in REST MockService

SOLVED
Highlighted
New Contributor

[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.

Tags (3)
1 ACCEPTED SOLUTION

Accepted Solutions
Valued Contributor

Re: [SoapUI 5.2.1] mockRequest is NULL in REST MockService

Hi,

 

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

Screen Shot 2016-04-28 at 13.14.50.png

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

 

Author of SoapUI Cookbook
14 REPLIES 14
Valued Contributor

Re: [SoapUI 5.2.1] mockRequest is NULL in REST MockService

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

Author of SoapUI Cookbook
New Contributor

Re: [SoapUI 5.2.1] mockRequest is NULL in REST MockService

Hi,

 

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

Valued Contributor

Re: [SoapUI 5.2.1] mockRequest is NULL in REST MockService

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

 

Author of SoapUI Cookbook
New Contributor

Re: [SoapUI 5.2.1] mockRequest is NULL in REST MockService

I tried this approach too. And this gives the following error :

 

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

Community Hero

Re: [SoapUI 5.2.1] mockRequest is NULL in REST MockService

May be you want to show the updated script which you get this issue. And what values are being passed in the request and what value you wanted to read in the script dispatcher?


Regards,
Rao.
Valued Contributor

Re: [SoapUI 5.2.1] mockRequest is NULL in REST MockService

Hi,

 

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

Screen Shot 2016-04-28 at 13.14.50.png

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

 

Author of SoapUI Cookbook
New Contributor

Re: [SoapUI 5.2.1] mockRequest is NULL in REST MockService

Thanks @rupert_anderson for the quick replies! I was able to work out a solution from all the feedback provided by you. Smiley Happy

New Member

Re: [SoapUI 5.2.1] mockRequest is NULL in REST MockService

How did you solve it ?

I still get the exact same problem on 5.3.0

 

ss.PNG

New Contributor

Re: [SoapUI 5.2.1] mockRequest is NULL in REST MockService

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.


 

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