Set HTTP Header value from a file

Gatling version: 3.13.1
Gatling flavor: java kotlin scala javascript typescript
Gatling build tool: maven gradle sbt bundle npm
Maven Version : Apache Maven 3.9.9
Java Version : JDK 11.0.25 (Adoptium OpenJDK Windows x64)
OS : Windows 11

I have defined some headers I want to pass in my HTTP request. However, I want to set 1 of the headers based on a value read from a CSV file in my resources directory:

private Map<CharSequence, String> headers = Map.ofEntries(
  Map.entry("Accept", "application/json"),
  Map.entry("Custom-Value", "some value I want to read from a file")
);

HttpProtocolBuilder httpProtocol =
    http.baseUrl("https://my-service.url.com")
      .contentTypeHeader("application/json");

ScenarioBuilder myFirstScenario = scenario("My First Scenario")
    .exec(http("Request 1")
    .post("/service/myapi")
    .headers(headers)
);

{
    setUp(
      myFirstScenario.injectOpen(constantUsersPerSec(2).during(60))
    ).protocols(httpProtocol);
}

I have tried to use a Feeder but not really making any progress. Please note that I only need the value once, & will use the same header value in every invocation.

did you try #{your-header} ?

Header names can’t be dynamic, only header values.

May be you can try with the below variables format -
//package to import
import io.gatling.http.Predef._

val variable =“some value I want to read from a file”

private Map<CharSequence, String> headers = Map.ofEntries(
Map.entry(HttpHeaderNames.Accept, HttpHeaderValues.ApplicationJson),
Map.entry(HttpHeaderNames.ContentType, HttpHeaderValues.ApplicationJson)),
Map.entry(“Custom-Value”, s"${variable}")
);

Hope this helps :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.