The HTTP protocol configuration is the foundation of your simulation. It defines everything that is common across all requests -- base URL, default headers, connection handling, proxy settings. Think of it as the "environment setup" for your test. In JMeter, you would configure these across multiple Config Elements. In Gatling, it is one fluent builder.
HttpProtocolBuilder httpProtocol = http
// Base URL -- all relative paths resolve against this
.baseUrl("https://api.example.com")
// Default headers (added to every request)
.acceptHeader("application/json")
.contentTypeHeader("application/json")
.acceptLanguageHeader("en-US,en;q=0.9")
.userAgentHeader("Gatling/PerfTest")
// Custom header (e.g., API key)
.header("X-Api-Key", "test-key-12345");HttpProtocolBuilder httpProtocol = http
.baseUrl("https://api.example.com")
.acceptHeader("application/json")
// Connection settings
.shareConnections() // Share connections across users (realistic)
.maxConnectionsPerHost(6) // Browser-like connection limit
// Redirect handling
.disableFollowRedirect() // Manual redirect control
// Proxy (useful for debugging with Charles/Fiddler)
// .proxy(Proxy("localhost", 8888))
// SSL -- disable checks for self-signed certs in staging
// .disableAutoReferer()
// Warm-up -- Gatling sends a request to detect issues early
.disableWarmUp() // Skip warm-up request
// Response body handling
.inferHtmlResources(); // Auto-fetch CSS, JS, images (like a browser)You can define multiple protocols for different scenarios. A common pattern: one protocol for the web frontend (HTML, browser headers) and another for the API backend (JSON, auth tokens).
HttpProtocolBuilder webProtocol = http
.baseUrl("https://www.example.com")
.acceptHeader("text/html")
.inferHtmlResources();
HttpProtocolBuilder apiProtocol = http
.baseUrl("https://api.example.com")
.acceptHeader("application/json")
.contentTypeHeader("application/json")
.header("Authorization", "Bearer #{authToken}");
// Use different protocols for different scenarios
{
setUp(
webScenario.injectOpen(rampUsers(200).during(Duration.ofSeconds(60)))
.protocols(webProtocol),
apiScenario.injectOpen(rampUsers(500).during(Duration.ofSeconds(60)))
.protocols(apiProtocol)
);
}Use inferHtmlResources() only for browser simulation tests where you want to mimic fetching CSS, JS, and images. For API tests, skip it -- it adds unnecessary requests and noise to your results. Most Gatling tests are API-level, where you do not need resource inference.
Key Point: The HTTP protocol builder centralizes all shared configuration. Define it once, attach it in setUp. Use multiple protocols when testing both web and API layers.
Key Point: HTTP protocol centralizes base URL, headers, and connection settings -- define once, attach in setUp