API testing - POST request with JSON Body parametrization

Hi All,
I need help to automate the Request body of an API POST method. I am having trouble with passing variables to the request body.

/* Request Body looks like this:*/

{

“customerName” : “string”,
“customerAddress1” : “string”,
“customerAddress2” : “string”

}
My script looks like this:

// enrollmentAPI_submitenrollment from object Repository

def request = ((findTestObject(‘enrollmentAPI_submitenrollment’)) as RequestObject)

/* Capturing data from Excel sheet rows (i have a loop to increment the rows in excel but thts not relevent to the issue so not providing tht info*/

def Name= findTestData(“ExcelData-1”).getValue(“ColumnName”,1)
def Address1= findTestData(“ExcelData-1”).getValue(“ColumnAddress1”,1)
def Address2= findTestData(“ExcelData-1”).getValue(“ColumnAddress2”,1)

def CustName= “customerName: ${Name}”
def CustAdd1= “customerAddress1: ${Address1}”
def CustAdd2= “customerAddress2: ${Address2}”

String body = “{${-> CustName},${-> CustAdd1},${-> CustAdd2}}”

println(body)

But this returns a body that looks like :

{ customerName : ${Name}, customerAddress1 : ${Address1}, customerAddress2 :${Address2}}

However when i submit the request like this:

request.setBodyContent(new HttpTextBodyContent(body, ‘UTF-8’, ‘application/json’))

im receiving a 400 error saying tis a bad request.

im trying to achieve a post request body like this:

{ “customerName” : “${Name}”," customerAddress1" : “${Address1}”, “customerAddress2” :“${Address2}”}

How can i get this? how can i parametrize just the names in the body to do the automation?

Plz advise

Thanks!

I defined the variables in the below fashion and it helped me to get the body in JSON format.

def CustName= ““customerName”: “${Name}””
def CustAdd1= “\“customerAddress1”: “${Address1}””
def CustAdd2= ““customerAddress2”: “${Address2}\””

everything else stayed the same.

Hi All,

I would need help to automate Request body of an API POST method.
// My request body

{
name: “string”,
movies: “string”
}

I am trying to get input from excel and publish request . Here is my script

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable

// sample is from objectRepository

def request = ((findTestObject(‘sample’)) as RequestObject)
TestData accounts = findTestData(‘Data Files/Accounts’)
def name= findTestData(“Accounts”).getValue(“Columnkm”,1)
def movies= findTestData(“Accounts”).getValue(“Columnkm”,2)

def nname= ““name”:”${name}""
def nmovies= ““movies”:”${movies}""

String body = “{${-> nname},${->nmovies}}”

println(body)

Script throws 'Reason:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
file:/C:/Users/kmaniv01/Katalon%20Studio/Customkwd/Scripts/sample/Script1570485157291.groovy: 16: unable to resolve class RequestObject
@ line 16, column 15.
def request = ((findTestObject(‘sample’)) as RequestObject)
^

1 error’

Could you please help me to resolve this