How to set body content in script for post request using builder

My Script is failing when i developed API testing manually in test case using Rest Request object builder and the same works properly when i user web service object.

Some how my script us failing at body content it is not at all reading.below is my code.
String body = ‘{ “name” : "’+NewBranchName+‘",“target” :{“hash” : "’+ParentBranchName+‘"}}’

println(body)
def requestObject = builder
.withRestRequestMethod(‘POST’)
.withRestUrl(URI)
.withHttpHeaders([new TestObjectProperty(‘Content-Type’, ConditionType.EQUALS, ‘application/json’)])
.withHttpHeaders([new TestObjectProperty(‘Authorization’, ConditionType.EQUALS, Authencoded)])
.withTextBodyContent(jsonbody)
.build()

the screen shot of the object which is working fine.

Please let me know where i am doing wrong.

Regards,
Rajesh Rapolu

1 Like

Can you show the rest of the script?
Are you sending the request with something like WS.sendRequest(requestObject,[('NewBranchName'):NewBranchName, ('ParentBranchName'):ParentBranchName])?

HI @ [Mate_Mrse]
@ the object level i am using global variable which is working fine.

But the same when i am using builder it is failing to read body content get request is working fine for the same URI but Post it is failing.

String body = ‘{ “name” : "’+NewBranchName+’",“target” :{“hash” : “’+ParentBranchName+’”}}’

println(body)

‘Create a new Get object using builder’
def requestObject = builder
.withRestRequestMethod(‘POST’)
.withRestUrl(URI)
.withHttpHeaders([new TestObjectProperty(‘Content-Type’, ConditionType.EQUALS, ‘application/json’)])
.withHttpHeaders([new TestObjectProperty(‘Authorization’, ConditionType.EQUALS, Authencoded)])
.withTextBodyContent(body)
.build()

def response = WS.sendRequest(requestObject)

this what i am using.

What error do you get for POST request?

below error i am getting

{error={fields={name=required key not provided, target=required key not provided}, message=Bad request}, type=error}
400

Actually, (most probably) there is a problem with your request body, the request itself is sent successfully and you got the error from a server. You miss some key there.

Exactly what might be the problem ?

with the same data it work by using web service object, I mean i am not getting this error …it working fine as expected

Is that a valid value for the given keyword? I don’t think so.
You have to review the body you build.

You can build it more accurate with the JsonOutput, see:
http://groovy-lang.org/json.html

Hi,

In test object i am using as plain text only which is working fine.

and in scripts when i print the body it is printing as expected.
i am concatenating and using as string in the body.

But send request is failing.

1 Like

You are trying to substitute some variables there, in the body, when you build it from script?
If so, don’t do it using single quotes (’’) but double quotes ("") to have the variable as GString, not plain java string.
And try not to use quotes inside the body, e.g “name” : “value” … I don’t know what the API you test is expecting so this last advise is just a guess.

Thank you all,
Actually i want to parameter everything in the webservice object but when i was building body i faced this issue.
Some how i had fixed it in below way.
Body i had maintained in web service object and in script i had modified web service object to parameter .

Regards,
Rajesh Rapolu

1 Like

Hi,

Can You tell me how do you placed json array in request body??

This worked for me. Thanks Rajesh.