Service Test 11: How To Set HTTP Request Headers & Get HTTP Response Headers

Recently one of our clients asked if it was possible to set HTTP Request Headers  and retrieve HTTP Response Headers in Service Test 11 for REST and SOAP activities. The test case began by executing an activity to authenticate and retrieve an authentication token, and that token had to be passed in an authentication header for every remaining activity. This is all possible in Service Test 11.20 – let’s take a look at how it’s done.

Setting HTTP Request Headers

SOAP Activities

For SOAP activities, C# code can be added to either the OnSendRequest or BeforeApplyProtocolSettings events listed in the activity Property Sheet.  The activity’s HttpRequestHeaders object Add method can directly add HTTP Request Headers. This method accepts two parameters, a key and value:

((StServiceCallActivity)args.Activity).HttpRequestHeaders.Add("<key>", "<value>");

Step 1:  Add the Event

  1. Click on the test activity in the Test Flow.
  2. In the activity Property Sheet, click the Events button.
  3. Double click in the Handler column for the OnSendRequest or BeforeApplyProtocolSettings event.
  4. The TestUserCode.cs file should open in Service Test and the event should be added.

Step 2:  Add C# Code

Add the following line of code inside the event function:

((StServiceCallActivity)args.Activity).HttpRequestHeaders.Add("Authorization", "1234567890");

Alternatively, you can directly access the method using the named activity object:

this.StServiceCallActivity40.HttpRequestHeaders.Add("Authorization", "1234567890");

 REST Activities

For REST activities, before adding C# code you must add a RequestHeader array element in the Input/Checkpoint section of the Property Sheet. C# code can then be added to the BeforeExecuteStepEvent event listed in the activity Property Sheet. The Request Header is directly accessed and set through the activity context:

this.HTTPActivity15.RequestHeaders[0].Key = @"<key>";
this.HTTPActivity15.RequestHeaders[0].Value = @"<value>";

You may ask this question:  If the Request Header is added through the Property Sheet, why do we need to use C# to set the array value?  Well, in most situations you don’t – at least not for the first array element. The first array element’s value can be directly linked to a Data Source, a Test Variable or an Available Step; however, additional array element values must be set programmatically if want to link them to a Data Source. This is a limitation in Service Test 11.

If you only have one RequestHeader array element, or if you are directly linking to a Test Variable or Available Step, link or set the value accordingly through the GUI. The steps below provide an example for setting the array element programmatically (when the GUI will not suffice):

Step 1:  Add the Request Header

  1. Click on the test activity in the Test Flow.
  2. In the activity Property Sheet, select the Input/Checkpoints button.
  3. Click the + button in the RequestHeaders row, expand RequestHeaders[1] and provide a value for the Name.

Step 2: Add the Event

  1. With the REST activity still selected, in the activity Property Sheet, click the Events button.
  2. Double click in the Handler column of the BeforeExecuteStepEvent event row.
  3. The TestUserCode.cs file should open in Service Test and the event should be added.

Step 3:  Add C# Code

Add the following line of code inside the event function:

// Array indexes start at 0, even though they begin at '1' in the Property Sheet
this.HTTPActivity15.RequestHeaders[0].Key = @"Authorization";  // This is not required; already set in property sheet
this.HTTPActivity15.RequestHeaders[0].Value = "01234567890";

Retrieving HTTP Response Headers

In the scenario described in the beginning on this post, the first activity in the Test Flow was an activity call to authenticate and receive an authentication token. In order to reuse the authentication token later in the test, the response can be stored as a Service Test Test Variable. Test Variables are global variables that can be accessed later in the test, either by linking to the variable (link to data source) or by retrieving the variable programmatically.

The examples in this section assume that you know how to set and retrieve Test Variable values programmatically. Take a look at our detailed post for more information.

SOAP Activities

The C# code below can be added in the activity AfterExecuteStepEvent event:


Alternatively, you can directly access the method using the named activity object:


To put it all together, the following code will set a Test Variable to the HTTP Response Header:

this.Context.TestProfile.SetVariableValue("AuthToken", ((StServiceCallActivity)args.Activity).HttpResponseHeaders.Get("AuthToken"));

REST Activities

REST is a little more complex. Service Test can access the raw request response. Using C# we must iterate through the HTTP Response Headers (raw response) to find the header element we need to store, remove the unrelated string data, and store the value in the Test Variable. The C# code below does this (which should be placed in the REST AfterExecuteStepEvent event):

String strResponseHeader = "";
Boolean blStringFound = false;
// Loop through raw the raw response headers
for (int i = 0; i <= this.HTTPActivity15.ResponseHeaders.Length-1; i++)
	// Get the response header
	strResponseHeader = this.HTTPActivity15.ResponseHeaders.GetValue(i).ToString();
	// Find the applicable response header
	if (strResponseHeader.IndexOf("AuthToken") > 0)
		// Remove brackets
		strResponseHeader = strResponseHeader.Remove(0, 1);
		strResponseHeader = strResponseHeader.Remove(strResponseHeader.Length - 1, 1);
		// Get the value of the response
		strResponseHeader = strResponseHeader.Substring(strResponseHeader.IndexOf(",") + 2);
		blStringFound = true;
// If found, set the Test Variable; otherwise, report failure.
if (blStringFound)
	this.Context.TestProfile.SetVariableValue("AuthToken", strResponseHeader);
} else {
	args.Activity.Report("*** Error ***", "AuthToken not found");


If the Service Test script uses a Test Variable to store an authentication token (or any other data) returned in an HTTP Response Header, you can reuse that Test Variable to set an HTTP Header. Simply reference the examples provided in this post as well as our post detailing how to set and retrieve Test Variable values programmatically. It is also possible to reuse the value store in the Test Variable in the GUI by linking to a data source and selecting the Test Variable:

What are your thoughts? Have you found other ways to set HTTP Headers or retrieve HTTP Responses?

What's Next?

Did you enjoy this article? Help spread the word by sharing:

Join the Northway Navigator Club today and get access to restricted content including our best tips and tricks. Membership is free! You will also receive free email updates by registering.

Engage in the conversation and leave a comment:

Brian MacKenzie

About Brian MacKenzie (24 articles)

Brian has over 14 years of experience working in IT, ranging from Software Quality Assurance, Software Development, Business Management to Systems Administration, in a wide range of technologies, languages, databases, systems and software. He has experience in both software quality assurance and software development roles. Brian specializes in HP LoadRunner, HP UFT (QuickTest Pro and Service Test), Mobile Testing and Automation, HP ALM and HP Service Virtualization, as well as delivering certified instructor training in HP UFT and mentoring in Service Test and Service Virtualization.

  • bhill

    Thank you Brian. The REST example in particular, has been very helpful. And can be reused for multiple activities when “HTTPActivity15.ResponseHeaders” is accessed from the sender or args input of the event handler :




    But what if I want to set a REST Output Property instead of a Test Variable?

    For example :
    this.RESTActivity14.Output.Token = strResponseHeader;
    How do I reference the RESTActivity14 using the sender or args input of the event handler?