# How to set HTTP header with variable?

**URL:** <https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436>\
**Category:** API Testing\
**Tags:** api-testing, katalon-studio\
**Created:** [February 19, 2018, 7:02am UTC](https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436 "2018-02-19T07:02:16Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mariusz](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/mariusz/32/386_2.png) [@Mariusz](https://forum.katalon.com/u/Mariusz)\
**Post date:** [February 19, 2018, 7:02am UTC](https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436/1 "2018-02-19T07:02:16Z")

</div>

Hello,  
I have a problem. What I want to do is to obtain a token from RESTFul request, and parse that token to HTTP Header.

Here is my code

```auto
import internal.GlobalVariable as GlobalVariable
import com.kms.katalon.core.testobject.RequestObject

'Get response'
def response = WS.sendRequest(findTestObject('API/Authentication'))

'Verify if login was successfully'
WS.verifyElementPropertyValue(response, 'msg.ok[0]', 'Login successfull!')

def slurper = new groovy.json.JsonSlurper()

def result = slurper.parseText(response.getResponseBodyContent())

'Get token'
def token = result.data."X-Auth-Token"

'Print token'
println(token)

RequestObject ro2 = new RequestObject("y")
ro2.setHttpBody("id": "ab8e52e92deb459596e3911f9f09934c")
ro2.setHttpHeaderProperties("X-Auth-Token" : token)
ro2.setRestRequestMethod("POST")
ro2.setRestUrl("http://localhost/api/projects/scopeTo/")

WS.sendRequest(ro2)

```

1. I’m authenticating.  
2. Verify whether there is a message in the response (Login successfull!)  
3. Parse response with JSONSlurper  
4. Save token to variable token

Now, when I have a token, I want to authenticate to another endpoint. What I need to do is to send in body an extra ID, and send in HTTP Header “X-Auth-Token” with token from variable.

Response from console:

```auto
02-19-2018 07:40:48 AM - [START] - Start action : Statement - println(token)
ojear3mndj1sarskbjl1r64dd5
02-19-2018 07:40:48 AM - [END] - End action : Statement - println(token)
02-19-2018 07:40:48 AM - [START] - Start action : Statement - ro2 = new com.kms.katalon.core.testobject.RequestObject(y)
02-19-2018 07:40:48 AM - [END] - End action : Statement - ro2 = new com.kms.katalon.core.testobject.RequestObject(y)
02-19-2018 07:40:48 AM - [START] - Start action : Statement - ro2.setHttpBody(["id":"ab8e52e92deb459596e3911f9f09934c"])
02-19-2018 07:40:48 AM - [END] - End action : Statement - ro2.setHttpBody(["id":"ab8e52e92deb459596e3911f9f09934c"])
02-19-2018 07:40:48 AM - [ERROR] - Test Cases/API/Authentication FAILED because (of) groovy.lang.MissingMethodException: No signature of method: com.kms.katalon.core.testobject.RequestObject.setHttpBody() is applicable for argument types: (java.util.LinkedHashMap) values: [[id:ab8e52e92deb459596e3911f9f09934c]]
Possible solutions: setHttpBody(java.lang.String), getHttpBody(), setSoapBody(java.lang.String), getSoapBody()

```

How should be that parsed and send to HTTP Header?

---

<div class="post-metadata">

**Author:** ![Trong\_Bui](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/trong_bui/32/374_2.png) [@Trong\_Bui](https://forum.katalon.com/u/Trong_Bui)\
**Post date:** [February 22, 2018, 4:21am UTC](https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436/2 "2018-02-22T04:21:39Z")

</div>

Mariusz,

The error you got from the console is: ro2.setHttpBody(“id”: “ab8e52e92deb459596e3911f9f09934c”) since the function requires a String. I am not sure how the API is setup, you could try to send it as a String, ie “id:ab8e52e92deb459596e3911f9f09934c” instead.

---

<div class="post-metadata">

**Author:** ![Mariusz](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/mariusz/32/386_2.png) [@Mariusz](https://forum.katalon.com/u/Mariusz)\
**Post date:** [February 22, 2018, 8:52am UTC](https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436/3 "2018-02-22T08:52:28Z")

</div>

Trong,

Thanks for tips. I’ve changed some part of code and split and moved some stuff to Test Suites and that is how it looks like now

```auto
'Get token'
String token = WebUI.callTestCase(findTestCase('API/Authentication'), [:], FailureHandling.CONTINUE_ON_FAILURE)

'Scope to a project'
RequestObject ScopeToProject = findTestObject('API/ScopeToProject')

'Create new ArrayList'
ArrayList<TestObjectProperty> HTTPHeader = new ArrayList<TestObjectProperty>()

'Send token in HTTP header'
HTTPHeader.add(new TestObjectProperty('X-Auth-Token', ConditionType.EQUALS, token))

'Set that token'
ScopeToProject.setHttpHeaderProperties(HTTPHeader)

'Get response text'
response = WS.sendRequest(ScopeToProject)

'Verify if scope to project was successfull'
WS.verifyElementPropertyValue(response, 'data.currentProject', 'ab8e52e92deb459596e3911f9f09934c')

```

Maybe someone may take advantage of this 🙂

---

<div class="post-metadata">

**Author:** ![Trong\_Bui](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/trong_bui/32/374_2.png) [@Trong\_Bui](https://forum.katalon.com/u/Trong_Bui)\
**Post date:** [February 22, 2018, 9:02am UTC](https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436/4 "2018-02-22T09:02:35Z")

</div>

Your script looks great. Thank you for sharing with us.

---

<div class="post-metadata">

**Author:** ![Ananth\_raghav](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Ananth\_raghav](https://forum.katalon.com/u/Ananth_raghav)\
**Post date:** [March 5, 2018, 1:49pm UTC](https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436/5 "2018-03-05T13:49:06Z")

</div>

> Mariusz said:
> 
> Trong,
> 
> Thanks for tips. I’ve changed some part of code and split and moved some stuff to Test Suites and that is how it looks like now
> 
> ```auto
> 'Get token'
> 
> ```

String token = WebUI.callTestCase(findTestCase(‘API/Authentication’), [:], FailureHandling.CONTINUE\_ON\_FAILURE)

‘Scope to a project’  
RequestObject ScopeToProject = findTestObject(‘API/ScopeToProject’)

‘Create new ArrayList’  
ArrayList HTTPHeader = new ArrayList()

‘Send token in HTTP header’  
HTTPHeader.add(new TestObjectProperty(‘X-Auth-Token’, ConditionType.EQUALS, token))

‘Set that token’  
ScopeToProject.setHttpHeaderProperties(HTTPHeader)

‘Get response text’  
response = WS.sendRequest(ScopeToProject)

‘Verify if scope to project was successfull’  
WS.verifyElementPropertyValue(response, ‘data.currentProject’, ‘ab8e52e92deb459596e3911f9f09934c’)

> ```auto
> 
> Maybe someone may take advantage of this :)  
> 
>   
> Thank you. This is what i was looking for!
> 
> ```

---

<div class="post-metadata">

**Author:** ![Ananth\_raghav](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Ananth\_raghav](https://forum.katalon.com/u/Ananth_raghav)\
**Post date:** [March 5, 2018, 1:49pm UTC](https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436/6 "2018-03-05T13:49:35Z")

</div>

It worked for my advantage 🙂

---

<div class="post-metadata">

**Author:** ![Ananth\_raghav](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Ananth\_raghav](https://forum.katalon.com/u/Ananth_raghav)\
**Post date:** [March 5, 2018, 1:52pm UTC](https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436/7 "2018-03-05T13:52:02Z")

</div>

Just to add to your code, these 2 import statements are required as well.

**import** com.kms.katalon.core.testobject.TestObjectProperty

**import** com.kms.katalon.core.testobject.ConditionType

---

<div class="post-metadata">

**Author:** ![tlili.hassna](https://avatars.discourse-cdn.com/v4/letter/t/f6c823/32.png) [@tlili.hassna](https://forum.katalon.com/u/tlili.hassna)\
**Post date:** [December 24, 2018, 1:19pm UTC](https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436/8 "2018-12-24T13:19:37Z")

</div>

I try to set HTTP header with a variable in the test case.  
I write this, but it doesn’t work

ArrayList HTTPHeader = new ArrayList()

HTTPHeader.add(new TestObjectProperty(((‘X - OAPI) - Application) - Id’, ConditionType.EQUALS, ‘1111’))

request = WSResponseManager.getInstance().getCurrentRequest()

request.setHttpHeaderProperties(HTTPHeader)

Thank you for your help

---

<div class="post-metadata">

**Author:** ![Marek\_Melocik](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/marek_melocik/32/371_2.png) [@Marek\_Melocik](https://forum.katalon.com/u/Marek_Melocik)\
**Post date:** [December 24, 2018, 1:28pm UTC](https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436/9 "2018-12-24T13:28:06Z")

</div>

I am a bit confused what is what in your request. And I can’t even see any variable in a header.

Please post here:

- header name
- header value
- variable and a place, where you want to put it

Also, brackets on the second line aren’t correct.

---

<div class="post-metadata">

**Author:** ![tlili.hassna](https://avatars.discourse-cdn.com/v4/letter/t/f6c823/32.png) [@tlili.hassna](https://forum.katalon.com/u/tlili.hassna)\
**Post date:** [December 24, 2018, 1:31pm UTC](https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436/10 "2018-12-24T13:31:50Z")

</div>

> [@Marek\_Melocik](#):
>
> I am a bit confused what is what in your request. And I can’t even see any variable in a header.

Header name: X-OAPI-Application-Id  
Header value ‘1111’ ( I will then change it by a variable later)

In this statement I give the name and the value of header  
HTTPHeader.add(new TestObjectProperty(‘X-OAPI-Application-Id’, ConditionType.EQUALS, ‘1111’))

---

<div class="post-metadata">

**Author:** ![Marek\_Melocik](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/marek_melocik/32/371_2.png) [@Marek\_Melocik](https://forum.katalon.com/u/Marek_Melocik)\
**Post date:** [December 24, 2018, 1:34pm UTC](https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436/11 "2018-12-24T13:34:29Z")

</div>

Yes, the row looks correct now.

And the other line, I guess you’d like to pick an existing request (from Object Repository), put a new header and run the request, right?

---

<div class="post-metadata">

**Author:** ![tlili.hassna](https://avatars.discourse-cdn.com/v4/letter/t/f6c823/32.png) [@tlili.hassna](https://forum.katalon.com/u/tlili.hassna)\
**Post date:** [December 24, 2018, 1:35pm UTC](https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436/12 "2018-12-24T13:35:55Z")

</div>

> [@Marek\_Melocik](#):
>
> And the other line, I guess you’d like to pick an existing request (from Object Repository), put a new header and run the request, right?

Yes an existing request from object repository, put the header and run it

---

<div class="post-metadata">

**Author:** ![Marek\_Melocik](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/marek_melocik/32/371_2.png) [@Marek\_Melocik](https://forum.katalon.com/u/Marek_Melocik)\
**Post date:** [December 24, 2018, 2:13pm UTC](https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436/13 "2018-12-24T14:13:49Z")

</div>

Ok so this should work for you:

```
ArrayList HTTPHeader = new ArrayList()

String headerValue = '1111'
HTTPHeader.add(new TestObjectProperty('(X - OAPI) - Application) - Id', ConditionType.EQUALS, headerValue))

RequestObject request = ObjectRepository.findTestObject("Misc/WStest")
request.setHttpHeaderProperties(HTTPHeader)

WSBuiltInKeywords.sendRequest(request)
```

---

<div class="post-metadata">

**Author:** ![chaiku3](https://avatars.discourse-cdn.com/v4/letter/c/ce7236/32.png) [@chaiku3](https://forum.katalon.com/u/chaiku3)\
**Post date:** [January 3, 2019, 6:43am UTC](https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436/14 "2019-01-03T06:43:15Z")

</div>

Hi Marek,

RequestObject request = WSResponseManager.getInstance().getCurrentRequest()  
ResponseObject response = WSResponseManager.getInstance().getCurrentResponse()

ArrayList HTTPHeader = new ArrayList()  
string headerValue = ‘Long\_access\_token’  
HTTPHeader.add(new TestObjectProperty(‘Authorization’, ConditionType.EQUALS, headerValue))  
request.setHttpHeaderProperties(HTTPHeader)

and then i click “Test Request and Verify”

but the status always 401 unauthorized

if i tried to put Authorization manually at HTTP Header then it will work fine.

can help?

---

<div class="post-metadata">

**Author:** ![yli](https://avatars.discourse-cdn.com/v4/letter/y/bbe5ce/32.png) [@yli](https://forum.katalon.com/u/yli)\
**Post date:** [January 24, 2019, 2:43pm UTC](https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436/15 "2019-01-24T14:43:19Z")

</div>

> [@Ananth\_raghav](#):
>
> setHttpHeaderProperties

The code does not compile with ‘setHttpHeaderProperties’ method, does this one require any Import particularly?  
Thanks

---

<div class="post-metadata">

**Author:** ![Arun\_Kumar](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@Arun\_Kumar](https://forum.katalon.com/u/Arun_Kumar)\
**Post date:** [February 8, 2019, 2:19pm UTC](https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436/16 "2019-02-08T14:19:49Z")

</div>

This helped a lot. This i was looking for hours.

---

<div class="post-metadata">

**Author:** ![D\_Adams](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/d_adams/32/3059_2.png) [@D\_Adams](https://forum.katalon.com/u/D_Adams)\
**Post date:** [March 18, 2019, 2:35pm UTC](https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436/17 "2019-03-18T14:35:16Z")

</div>

**Here are a couple of keyword functions to get a token and set the property in the request.**

```
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.annotation.Keyword
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.TestObjectProperty
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

@Keyword
def String getToken(){
	def authResponse = WS.sendRequestAndVerify(findTestObject('Auth/AuthenticateUser'))
	def slurper = new groovy.json.JsonSlurper()
	def result = slurper.parseText(authResponse.getResponseBodyContent())
	return result.token
}

@Keyword
def ResponseObject sendAuthorizedRequest(RequestObject restRequest, String access_token){

	// Build the authentication header
	def authRequest = restRequest
	ArrayList<TestObjectProperty> httpHeader = new ArrayList<TestObjectProperty>()
	httpHeader.add(new TestObjectProperty('Authorization', ConditionType.EQUALS, "Bearer " + access_token))

	// Set the header property and pass it back out
	authRequest.setHttpHeaderProperties(httpHeader)
	def response = WS.sendRequestAndVerify(authRequest)
	return response
}

```

**And to use it in a test case, I call it like this:**

```
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

def token = CustomKeywords.'tokens.TokenUtilities.getToken'()
def secureResponse = CustomKeywords.'tokens.TokenUtilities.sendAuthorizedRequest'(findTestObject('SystemUser/GetSystemUsers'), token)

WS.verifyResponseStatusCode(secureResponse, 200)
WS.verifyElementPropertyValue(secureResponse, 'data[0].registeredEmailAddress', 'samplename@samplemail.com')

```

This approach has worked for me so far…I hope it helps.

---

<div class="post-metadata">

**Author:** ![sachin.k](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@sachin.k](https://forum.katalon.com/u/sachin.k)\
**Post date:** [May 21, 2019, 6:06am UTC](https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436/18 "2019-05-21T06:06:28Z")

</div>

hello friends,

Based on POST method sign-in API i would like to pass my response(access token & token type) of sign-in API into authentication of next API  
i would like to pass it in a script mode not manually , i have declared the response as global variable  
access token in dynamic every time i hit sign-in API access token changes

so how to set the dynamic acess token and token type(Bearer) as http header in script mode please help

---

<div class="post-metadata">

**Author:** ![sudharani.suggu](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@sudharani.suggu](https://forum.katalon.com/u/sudharani.suggu)\
**Post date:** [June 26, 2019, 12:18pm UTC](https://forum.katalon.com/t/how-to-set-http-header-with-variable/9436/19 "2019-06-26T12:18:00Z")

</div>

> [@Mariusz](#):
>
> “X-Auth-Token”

‘Get response’  
def response = WS.sendRequest(findTestObject(‘EMP/EMP Login’))

‘Verify if login was successfully’  
WS.verifyElementPropertyValue(response, ‘result’, ‘Successful’)

WS.verifyResponseStatusCode(response, 200)

def slurper = new groovy.json.JsonSlurper()

def result1 = slurper.parseText(response.getResponseBodyContent())

println(result1)

‘Get token’  
def token1 = result1.‘token’  
println(token1)

‘Scope to a project’  
RequestObject ScopeToProject = findTestObject(‘EMP/GetAllTenants’)

‘Create new ArrayList’  
ArrayList HTTPHeader = new ArrayList()

‘Send token in HTTP header’  
HTTPHeader.add(new TestObjectProperty(‘Authorization’, ConditionType.EQUALS,'Bearer ’ +token1))

‘Set that token’  
ScopeToProject.setHttpHeaderProperties(HTTPHeader)

‘Get response text’  
response = WS.sendRequest(ScopeToProject)

‘Verify if scope to project was successfull’

WS.verifyResponseStatusCode(response, 200)

i used this… it worked for me.
