Setting HTTP Body during runtime

Option 3 (the way i use more often) - using a generic test object, parametrized:

and into the test-case I have something like this (I am passing the json body from an external file to keep the script cleaner)

'Given'
WS.comment("Setup")
reqUrl = "http://your/url/here"
reqBody = new File('./path/to/json/file').text
myToken = 'Bearer blah Blah'
request = findTestObject('POSTRequest.ContentJSON(url,body,token)', [ ('url') : reqUrl, ('body') : reqBody, ('token') : myToken ])

'When'
WS.comment("Sending POST request: ${request.getRestUrl()}")
WS.comment("With body: ${request.getHttpBody()}")
response = WS.sendRequest(request)

'Then'
WS.comment("Check response status")
WS.verifyResponseStatusCode(response, 201)

req_body.png

req_header.png

3 Likes