Ask a Question

Add multiple values from a table to request headers

Occasional Contributor

Add multiple values from a table to request headers

Is there a method for adding values from multiple records from a table (or any source) to a request name/value combination (header for instance) without knowing the "Name" and "Value" values prior to loading them?
SQL table name: RequestHeader
SQL table Fields: ID, HeaderKey, HeaderValue

In ReadyAPI: A POST request where "Name" and "Value" properties under the "Request" tab in the request must be populated from the table.
"Name" = RequestHeader.HeaderKey

"Value" = RequestHeader.HeaderValue

There could be zero or more entries in this "RequestHeader" table for multiple, discrete test cases.  The name and value of each header are unknown to the ReadyAPI request prior to being loaded to the "Name/Value" properties in the "Request" tab from the table at run time.  I want to be able to load multiple records from the table to the request header so that value from the "HeaderKey" field in each record loads to a discrete "Name" property and the value from "HeaderValue" field in each record loads to the associated "Value" property.  Can this be done in the provided ReadyAPI request object or will this require a request that is scripted in Groovy?  If this request must be scripted, are there any examples of this specific scenario?

Community Hero

Hey @em_qdx,

If im understanding your scenario, the embedded functionality of ReadyAPI wont support what you want to do - you can parameterise parm values, but you cant parameterise parm names.

So it's definitely groovy if im understanding what you want to do.

Just curious....what sort of requests have custom headers with varying custom parameter names? I've never come across this before....custom headers, yes. But always specific clearly defined custom headers.

Anyway, im guessing what youd need to do is use the Datasource step (data connection type) and build you request dynamically with groovy. Depending on what you need, you might need to park off the Datasource teststeps query results into a Properties teststep or myabe a DataGrid and once youve got your data saved, then dynamically build up your request sourcing the relevant data. I think i can do this in groovy (essentially cos @nmrao did this for me waaaay back), but my groovy sucks and the groovy experts are better placed to help with this than i am! (@ChrisAdams / @nmrao, etc.)
Happy to try and help if theyre too busy, etc., but you really want them helping you with your groovy rather than me!


if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta



The below Groovy script shows how to set headers for a given request.


It's a real simple example that just shows how to set key-value pairs.


// Need this to build the object for the headers...

// Get the test step of interest...
def testStep = testRunner.testCase.getTestStepByName("REST Request");

// Get the request object fo rthe test step
def testRequest = testStep.testRequest;

// Build our headers using StringToStringMap.  This bit could be made more custom by using a datasource, but for simplicity hard-code two values.
def newHeaders = new StringToStringMap();
newHeaders.put("MyCustomerHeaderOne", "MyCustomHeaderValueOne");
newHeaders.put("MyCustomerHeaderTwo", "MyCustomHeaderValueTwo");
newHeaders.put("MyCustomerHeaderThree", "MyCustomHeaderValueThree");

// Set the request headers with our newHeaders.
// Note : Default headers are still there.  E.g. Connection, Accept-Encdoding, but you could use this method to override the default set if you so wish


Which results in the headers being assigned to the test request.  The below screenshot is from the 'Raw' tab of the request.



Occasional Contributor

About the headers.  The names of the headers are defined within the APIs themselves, so they don't actually vary from request to request for the same API.  I created a database for data driven testing.  With that in mind, part of the design is to use the same ReadyAPI test for as many test cases as possible.  That means making that one ReadyAPI functional test as generic as possible.  One of the ways to do that is to use an EAV (Entity-Attribute-Value) approach to some of the tables, including the table that stores the headers for each test case.  Instead of having a table that contains a lot of columns, one for each specific header in each API, I have a header table that only has two columns instead: One column for the name of the header and the second for the value (EAV approach).  That is why I need to create a request that can load multiple headers with associated values from that table, but without knowing what the name and value of each header is prior to run time of the request.  It appears the only way to do this is to script the request itself, even if I use embedded functionality for data selects, validations, etc...Looks like I'll need to research Groovy and scripting a request test step. Thanks for the response.



My example may be a bit simple and use hard-coded values, but it was just an example to show how to set headers.


There's absolutely no reason why the example cannot be extended to use ODBC to look up the headers table and then add each applicable header for the test to the service call.



Occasional Contributor

@ChrisAdams - Would you be able to expand on your existing code example?  I need to retrieve all of the header rows (usually more than one) from an existing Data Source step (embedded, not scripted) and then add the values to headers in a POST request step.  Sorry if that wasn't clear from my original example.  I'm new to using Groovy and I'm having a difficult time finding relevant examples.  I'm also not having much luck finding a list/library of functions/methods available.  The auto complete feature for a line of code I attempted to write ("testRunner.testCase.testSteps["Header Table"].) didn't provide me with a method of retrieving the number of result rows, for instance, and I couldn't find any reference to this in a web search either.  Any help would be appreciated.  Thanks.



Are your headers in a datasource?  You mentioned database at the start of the thread.

Can you share a screenshot of your test steps so far and highlight where the headers are listed?

The Groovy will be different for each of the above, so I just want to be sure.


Occasional Contributor

@ChrisAdams -


Yes, the header records are selected in the "Test Header Select" data source.  This Data Source step to select the header records occurs immediately before the "Eligibility_With_Rules_Request" request step where the headers are required.

I could create a more complex query at the beginning of the test case to select all of the data I need across multiple tables, but for reasons specific to our business, and for the sake of simplicity, I have broken the selects out into simpler select statements in different Data Sources that occur just prior to each step that requires data from that table(s).  The initial "Automation Test Select" Data Source selects all of the test cases required for the test case. The subsequent selects/Data Sources use a foreign key in each of the associated tables to perform a select based on the primary key in the "Automation Test Select" Data Source result set.  Since the test case iterates through the result set from Automation Test Select, each subsequent select statement in the other Data Sources selects only the records in the other tables associated with the current record from the "Automation Test Select" Data Source.  Each "HeaderKey" from the table would be passed to the "Header" request header field and each "HeaderValue" would be passed to the "Value" request header field.  I hope that clarifies how this test case is structured, and how I am attempting to get the header data I require for each iteration of this test case. The headers I'm attempting to pass from the Data Source are in addition to the headers already shown in this screenshot.



The select statement from the "Test Header Select" Data Source:
Select TestRequestHeader.AutomationTestId as TestRequestHeader_AutomationTestId,
TestRequestHeader.TestRequestHeaderKey as TestRequestHeader_TestRequestHeaderKey,
TestRequestHeader.TestRequestHeaderValue as TestRequestHeader_TestRequestHeaderValue
From TestRequestHeader
Where TestRequestHeader.AutomationTestId = ${Automation Test Select#AutomationTest_AutomationTestId}




Gotcha.  One more question, the headers datasource.  When it runs, the query obtains all headers required for the next service call?  I ask as I'd need to think of a way to iterate over each header and add it to he request before submitting the request itself.



Occasional Contributor

@ChrisAdams - Yes, the "Test Header Select" Data Select that retrieves the headers will select all of the header records for the associated test case record selected in "Automation Test Select" (the current test record/iteration).  So the script that adds the headers to the request must be able to write/add multiple Header/Value combinations from the multiple header records selected to the request, while also not overwriting the existing hardcoded headers already in the request.  I hope that makes sense.

Showing results for 
Search instead for 
Did you mean: