Ask a Question

Getting null on mockRequest.getRequestContent() in one of REST services

MaxRybalkin91
Visitor

Getting null on mockRequest.getRequestContent() in one of REST services

Hello! I have several mock services in my SOAP project. I use the last version of SOAP UI (5.7.0)
One of the services contanis two methods with same path:

MaxRybalkin91_0-1643235130108.png

I have the same groovy script in both actions:

import groovy.json.JsonOutput
import groovy.json.JsonSlurper

class Response {
    Integer code
    String message
    String accountId
}

try {
    def requestBody = mockRequest.getRequestContent()
    def requestJson = new JsonSlurper().parseText(requestBody)
    def phone = requestJson.phone as String
    def email = requestJson.email as String
    def response
    if (phone == null || phone.trim() == "") {
        response = new Response(code: 1011, message: 'Field phone is required')
    } else if (phone == "+79777777777") {
        response = new Response(code: 1021, message: 'The phone field must be in the format +79991112233')
    } else if (email == "testt@testt.com") {
        response = new Response(code: 1022, message: 'The email field must be in the format test@test.com')
    } else {
        response = new Response(code: 0, message: 'Success', accountId: UUID.randomUUID().toString())
    }
    requestContext.response = JsonOutput.toJson(response).toString()
    return "response"
} catch (Exception ex) {
    requestContext.response = JsonOutput.toJson(new Response(code: 500, message: ex.message)).toString()
    return "response"
}

 My problem is that I'm getting error only in PUT method:

MaxRybalkin91_1-1643235353271.pngMaxRybalkin91_2-1643235367804.png

Another interesting fact that if I move my 'PUT' action from 'kaspersky' service to 'invest' or another, I won't get this error:

MaxRybalkin91_3-1643235529801.pngMaxRybalkin91_4-1643235590365.png

MaxRybalkin91_5-1643235753680.png

Creating another action with another path (not 'kaspersky') doesn't help. What can be a problem?

1 REPLY 1
KarelHusa
Champion Level 3

Hi @MaxRybalkin91 ,

this seems to be an old bug of PUT operation in REST mocks.

 

It was already discussed here: https://community.smartbear.com/t5/ReadyAPI-Questions/Mock-Service-mockRequest-requestContent-is-NUL... .

 

And the thread suggests a workaround (not nice, but working):

InputStream is = mockRequest.request.inputStream
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder sb = new StringBuilder()
while((s=br.readLine())!=null) {
sb.append(s)
}
String payLoad = sb.toString()

 

Best regards,

Karel

Karel@apimate.eu
https://apimate.eu
cancel
Showing results for 
Search instead for 
Did you mean: