Hello guys, In our previous post Accelerate performance with Gatling:3.3.0-Warmup we explained:
- How to setup gatling-sbt project
- How to send a GET request with gatling
- How to assert on the status code and other response attributes.
In this post, we will see what are the different ways to send an HTTP POST request with Gatling with different types of request bodies.
The HTTP POST method is used to send data to the server to create a new resource and sometimes used to update an existing resource. The type of the body of the request is indicated by the Content-Type header. You can add a full body to an HTTP request with the dedicated method body(body), where request body can be:
- RawFileBody
- ELFileBody
- StringBody
Let’s see how to send each type of request body with the POST request. To demonstrate the use-case, I have used a public API ‘POST Create User’, that will take a JSON request body with email and JOB profile and return an HTTP status code 201. Attaching a curl request for the quick reference.
curl --location --request POST 'https://reqres.in/api/users' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "Zula Steuber III",
"job": "Technician"
}'
Above API returns a valid JSON response with HTTP status 201 ‘Created’ as following:
{
"name": "Zula Steuber III",
"job": "Technician",
"id": "442",
"createdAt": "2020-05-03T18:32:27.265Z"
}
Let’s get started:
1) Create POST request in Gatling- StringBody Method:
This is pretty straight forwards and easiest way to send the request body as a raw string. This method is useful for the request with small request bodies.
.body(StringBody(string: Expression[String]))
//here argument string can be a raw String, a Gatling EL String, or an Expression function.

2) Create POST request in Gatling- RawFileBody Method:
RawFileBody method enables you to send the request bodies from an external raw file. Gatling will read the data from the file and inject it into the simulation without putting it in the memory. Following is the syntax for the same:
.body(RawFileBody(path: Expression[String])) // here argument path is the location of a file that will be uploaded and contains the data.

3) Create POST request in Gatling- ELFileBody Method:
ELFileBody method is majorly used to achieve data-driven frameworks capabilities. Gatling provides a way to put the request bodies in an external file where values of JSON attributed will be replaced by a placeholder called Gatling ELString.
The request body with ELString will look like
{"name": "${userFullName}","job": "${JobProfile}"}
Following is the syntax for the same: .body(RawFileBody(path: Expression[String])) // here argument path is the location of a file that will be uploaded and contains the data.

Lets Execute the test with command:
$ sbt compile 'gatling:testOnly rapidAutomation.PostUsers'
You will see something like this in the terminal Gatling execution logs:


So this is all, hope this post will help you to understand how to send POST request with Gatling, In next post we will discuss about GATLING feeders, Different injection profiles and Gatling throttling. Thank you.
References:
