Ora

How do you post a request in Gatling?

Published in Gatling HTTP Requests 3 mins read

To post a request in Gatling, you utilize the post() method within your HTTP call definition, configuring it with the necessary parameters for form data or multipart content.

How to Post a Request in Gatling

Posting requests in Gatling involves using specific methods within your simulation script to send data to a server, typically through post() calls configured with relevant parameters. This is fundamental for simulating user interactions that involve submitting data, like login forms, user registrations, or file uploads.

Gatling's HTTP DSL provides a clear and concise way to define these requests as part of your user scenarios.

1. Posting Standard Form Data

For typical POST requests where you send key-value pairs, often mimicking an HTML form submission, Gatling uses the formParam() method. This is suitable for content types like application/x-www-form-urlencoded.

Gatling POST Syntax for Form Data:

post("request-url").formParam("key", "value")

You can chain multiple formParam() calls to send several key-value pairs.

Example:

import io.gatling.core.predef._
import io.gatling.http.predef._
import scala.concurrent.duration._

class FormPostSimulation extends Simulation {

  val httpProtocol = http
    .baseUrl("http://your-api.example.com") // Your base URL
    .acceptHeader("application/json")
    .contentTypeHeader("application/x-www-form-urlencoded")

  val scn = scenario("Post Form Data Scenario")
    .exec(http("Submit User Details")
      .post("/api/users") // The specific endpoint for the POST request
      .formParam("username", "testuser") // First form parameter
      .formParam("email", "[email protected]") // Second form parameter
      .formParam("password", "securepassword123") // Third form parameter
    )

  setUp(
    scn.inject(atOnceUsers(1))
  ).protocols(httpProtocol)
}

In this example, the Submit User Details request will send username, email, and password as form parameters to the /api/users endpoint.

2. Posting Multipart Requests (e.g., File Uploads)

When you need to send complex data, such as file uploads along with other form fields, you'll use a multipart request. Gatling supports this to simulate scenarios like profile picture uploads or document submissions.

Gatling POST Syntax for Multipart Requests:

To initiate a multipart request, you start with http("Request Name").post("request-url"). Following this, you then define the parts of your multipart body, which can include files (RawFileBody, ElFileBody, FileBody), and other string-based form fields (StringBody).

Example (File Upload):

import io.gatling.core.predef._
import io.gatling.http.predef._
import scala.concurrent.duration._

class MultipartPostSimulation extends Simulation {

  val httpProtocol = http
    .baseUrl("http://your-upload-server.example.com") // Your base URL for file uploads

  val scn = scenario("Multipart File Upload Scenario")
    .exec(http("Upload Document")
      .post("/upload") // The specific endpoint for file upload
      .asMultipartForm // Explicitly marks the request as multipart form
      .bodyPart(RawFileBody("document", "data/sample_document.txt").fileName("my_uploaded_file.txt").contentType("text/plain"))
      .bodyPart(StringBody("description", "This is a sample document."))
    )

  setUp(
    scn.inject(atOnceUsers(1))
  ).protocols(httpProtocol)
    .assertions(
      global.successfulRequests.percent.is(100)
    )
}

In this example:

  • asMultipartForm ensures the request is sent with the multipart/form-data content type.
  • bodyPart(RawFileBody("document", "data/sample_document.txt").fileName("my_uploaded_file.txt").contentType("text/plain")) defines a file part named "document" that uploads sample_document.txt from the data directory (relative to your Gatling simulation).
  • bodyPart(StringBody("description", "This is a sample document.")) adds a regular form field named "description" to the multipart request.

Summary of POST Request Types

Request Type Description Key Gatling Syntax
Standard Form POST Sending simple key-value pairs (e.g., login credentials) via x-www-form-urlencoded. post("url").formParam("key", "value").formParam("anotherKey", "anotherValue")
Multipart POST Sending mixed data, typically including files, via multipart/form-data. post("url").asMultipartForm.bodyPart(RawFileBody(...)).bodyPart(StringBody(...))

By understanding these fundamental ways to post requests, you can effectively simulate diverse user behaviors in your Gatling performance tests.