Dynamic http header keys

Hi all,

I have a lot of dynamic http requests data stopred in DB. One of stopred data is http headers (along with method, body, url an so on)
I’m trying to format http request dynamicaly using jdbc feeder, like this:

val scn: ScenarioBuilder = scenario(“Scenario_1”)
.feed(dbFeeder)
.doSwitch("${method}")(
“POST” → {
exec({
http(“send_request_POST”)
.post("${uri}")
.headers(???)
.body("${body}")
.check(status.is(200))
})
},

I’m stick with headers. As I undestend I could not use scala method from gatling dsl (like .headers("${headers}")).

I tried get headers from session and store value in var like this:

val scn: ScenarioBuilder = scenario(“Scenario_1”)
.feed(dbFeeder)
.doSwitch("${method}")(
“POST” → {
exec({
var map: Map[String, String] = Map.empty
http(session => {
map = headersMapFromSession(ses)
“send_request_POST”
})
.get("${uri}")
.headers(map)

but I got empty map in headers.

Number of header keys is not fixed for every request and can not be filled manualy so code

.header(HttpHeaderNames.COOKIE, “${cookie}”)
.header(HttpHeaderNames.ACCEPT, “${accept}”)

will be failed if any header key if empty

Do you have any idea how can I fill headers dynamicaly?

Hi DV,

What you can do is get the list of possible headers you may have separately and store it in session values.

.check(headerRegex("//regex statement").find.optional.saveAs(“cookie1”))

if you use optional , it won’t show any failures.

So at any given point if 10 is the maximum response headers you may get from the API, you can store it separately and use it in the desired request.

This may not be the desired/efficient solution but this will do the trick.

Regards,
Aravind

Hi Aravind,
Thanx for your response. If I understand the documentation correctrly .check() is used after http request was sent. And using check() I can get only response headers.
But I’m trying to fill headers dynamicaly for initial request (request to the API).
I tried to fill all headers in session like this:

.doIf(session => session(“accept”).asOption[String].isEmpty) {
exec(session => session.set(“accept”, Option.empty))
}
.doIf(session => session(“cookie”).asOption[String].isEmpty) {
exec(session => session.set(“cookie”, Option.empty))
}

… and so on

but for example if “accept” header is not stored in DB - request header looks like this “accept”:“None”

суббота, 15 мая 2021 г. в 06:21:41 UTC+3, ara...@gmail.com:

Hi DV,

Now I understood. May be you can write an separate Scala function to get the required data from Database and then perform certain string operations to get the desired data and then feed it to your API request.

Regards,
Aravind G