Setting a header

Hello, I am new to katalon. Trying to add a custom header for my requested object. Such that when i get my response my header is visible before the body. This is my simple code:
response = WS.sendRequest(findTestObject(‘User/GetUserById’, [(‘id’) : 1, (‘variable_0’) : ‘’, (‘variable_1’) : ‘’, (‘variable_2’) : ‘’
, (‘variable_3’) : ‘’]))
data = response.getResponseText()

System.out.println(data)

Thanks for help

1 Like

I found a solution for my problem. I’ll share it with you here in this code:
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject
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.testng.keyword.TestNGBuiltinKeywords as TestNGKW
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 com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import internal.GlobalVariable as GlobalVariable
import org.openqa.selenium.Keys as Keys
import com.kms.katalon.core.util.KeywordUtil
import groovy.xml.XmlUtil
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.testobject.RestRequestObjectBuilder
import com.kms.katalon.core.testobject.TestObjectProperty
import com.kms.katalon.core.testobject.impl.HttpTextBodyContent
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.testobject.TestObject

response = WS.sendRequest(findTestObject(‘User/AddUser’), FailureHandling.STOP_ON_FAILURE)

WS.verifyResponseStatusCode(response, 200)

//getting id value out of previous ipa in order to put in new object header
String jsonString = WS.getElementPropertyValue(response, ‘id’, FailureHandling.CONTINUE_ON_FAILURE)

System.out.println(jsonString)

//passing variable id to the new ipa to get user
response = WS.sendRequest(findTestObject(‘User/GetUserById’,[(‘id’):jsonString]),FailureHandling.CONTINUE_ON_FAILURE)

data = response.getResponseText()

System.out.println(data)

//assigning new arraylist for header values
ArrayList HTTPHeader = new ArrayList()

//Scooping to the desired project ipa
RequestObject ScopeToProject = findTestObject(‘User/GetUserById’,[(‘id’):jsonString])

//Assigning some custom header properties in case of token or auto generated id as “jsonString”
HTTPHeader.add(new TestObjectProperty(‘ID’, ConditionType.EQUALS,jsonString ))

HTTPHeader.add(new TestObjectProperty(‘Authorization:type’, ConditionType.EQUALS,‘OAuth 1.0’))

HTTPHeader.add(new TestObjectProperty(‘Authorization’, ConditionType.EQUALS, ‘bearer’ + jsonString))

//pass arraylist to project header
ScopeToProject.setHttpHeaderProperties(HTTPHeader)

//printing user new header properties
for ( int i=0; i<3; i++) {
System.out.println(ScopeToProject.httpHeaderProperties.get(i).value)
}
response1 = WS.sendRequest(ScopeToProject)

data1 = response1.getResponseText()

System.out.println(data1)

//printing response header which is different than our assigned header for request “response header does not equal to request header”
System.out.println(response1.getHeaderFields())

This solution is implemented by me hope it helps you since that i found many questions concerning this topic.

It would be nice if you use “Code Formatting” syntax in the post.