Web Services Builder - needed clarifying

Hi! Could anyone explain to me usage of the WS Builder with different request methods. I am trying this:

'Create a new POST object using builder'def builder = new RestRequestObjectBuilder()def reqOrders = builder	.withRestRequestMethod('POST')	.withRestUrl('URL')	.withHttpHeaders([		new TestObjectProperty('Authorization', ConditionType.EQUALS, parsedUserToken),		new TestObjectProperty('Content-Type', ConditionType.EQUALS, 'application/json')	])	.withRestParameters([		new TestObjectProperty('service', ConditionType.EQUALS, 'value'),		new TestObjectProperty('userId', ConditionType.EQUALS, 'value'),
		new TestObjectProperty('currency', ConditionType.EQUALS, 'value'),
		new TestObjectProperty('price', ConditionType.EQUALS, 'value'),
		new TestObjectProperty('amount', ConditionType.EQUALS, 'value'),
		new TestObjectProperty('coins', ConditionType.EQUALS, 'value'),
		new TestObjectProperty('lang', ConditionType.EQUALS, 'EN'),		new TestObjectProperty('returnPage', ConditionType.EQUALS, 'value')	])	.build()'Send a request'def respOrders = WSBuiltInKeywords.sendRequest(reqOrders)

As a result I’ve receive this failure:

FAILED because (of) Unable to send request (Root cause: java.lang.NullPointerException)

In the examples from this link: https://docs.katalon.com/display/KD/Web+Services+Builder builder of “GET” method using “.withRestParameters” and “new TestObjectProperty”, builder of “POST” method using “.withUrlEncodedBodyContent” and “new UrlEncodedBodyParameter” - maybe that is my problem?

Thanks!

You are not using the actual URL variable, but rather than a String instead:

	.withRestUrl('URL')

Vinh Nguyen said:

You are not using the actual URL variable, but rather than a String instead:

	.withRestUrl('URL')

Vinh, I’ve changed my actual URL by ‘URL’ just for publishing my code in topic. I’m doing exactly as in example from Docs:

 .withRestUrl("http://jsonplaceholder.typicode.com/comments")

Ah I see. So looks like you are missing ‘HTTP Body’ content of this request, which it should be for a POSt object, example:

def builder = new RestRequestObjectBuilder();

def requestObject = builder

.withRestRequestMethod(“POST”)

.withRestUrl(“https://sample-web-service-aut.herokuapp.com/api/users/json”)

.withHttpHeaders([

new TestObjectProperty(“Content-Type”, ConditionType.EQUALS, “application/json”)

])

** .withTextBodyContent(’{“age”: 0, “gender”: “MALE”, “password”: “def”,“username”: “abc”}’)**

.build()

1 Like

Thanks, Vinh! It works! Last question: how can I use variables in this case (I think I can’t):

.withTextBodyContent('{"age": 0, "gender": "MALE", "password": "def","username": "abc"}')

Thanks a lot!!!

Never mind, I’ve figured out B)

Ivan Gorchakov said:p

Never mind, I’ve figured out B)

Could you share how?

First variant (pay attention - GlobalVariable is a String in this case):

.withTextBodyContent('{"phone": "'+GlobalVariable.myPhoneKS+'", "sharesQuantity": 10}')

Second variant (pay attention - GlobalVariable is a Int in this case):

.withTextBodyContent('{"userId": '+userIdKS+', "currency": "eur"}')

1 Like

Ivan Gorchakov said:

First variant (pay attention - GlobalVariable is a String in this case):

.withTextBodyContent('{"phone": "'+GlobalVariable.myPhoneKS+'", "sharesQuantity": 10}')

Second variant (pay attention - GlobalVariable is a Int in this case):   

.withTextBodyContent(‘{“userId”: ‘+userIdKS+’, “currency”: “eur”}’)


  

Nice one, thanks