Hi, I just started using Gatling in the last few days to test our API and am having some trouble with sending a body through a POST request.
After a few hours of head scratching i noticed that the “content-type” header is not being sent at all when I use the .body(…) expression in ChainBuilder.
I am using gatling 3.10.0 and used the Java Gradle sample project as the base. I have managed to reproduce the same error with using the example computerdatabase api.
I tried 3 different scenarios:
- Sending a POST request with just the .header(“content-type”, “application/foo+json”)
ChainBuilder noBodyCtHeaderExec = exec(
http("Form")
.post("/computers")
.header("content-type", "application/foo+json")
.check(status().is(200))
).exitHereIfFailed();
This provides me with the following output when i set response log level to TRACE:
POST /computers HTTP/1.1
Content-Length: 5000000
accept-language: en-US,en;q=0.5
content-type: application/foo+json
accept-encoding: gzip, deflate
accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/119.0
host: computer-database.gatling.io, content=null}
Everything seems fine up to this point.
- Sending a POST request with the .body() set to a .json file that contains some credentials and again setting the “content-type” header.
ChainBuilder bodyCtHeaderExec = exec(
http("Form3")
.post("/computers")
.body(RawFileBody("credentials.json"))
.header("content-type", "application/foo+json")
.check(status().is(200))
).exitHereIfFailed();
This provides me with the following console output:
POST /computers HTTP/1.1
Content-Length: 5000000
accept-language: en-US,en;q=0.5
accept-encoding: gzip, deflate
accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/119.0
host: computer-database.gatling.io, content=null}
Here the “content-type” header is not sent at all in the request.
- Sending a POST request with the body() set to a .json file that contains some credentials and using the asJson() which should set the “Accept” and “Content-type” headers automatically.
ChainBuilder asJsonExec = exec(
http("Form2")
.post("/computers")
.body(RawFileBody("credentials.json"))
.asJson()
.check(status().is(200))
).exitHereIfFailed();
Output:
POST /computers HTTP/1.1
Content-Length: 5000000
accept-language: en-US,en;q=0.5
accept-encoding: gzip, deflate
accept: application/json
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/119.0
host: computer-database.gatling.io, content=null}
Again the content-type header is nowhere to be seen, but the “Accept” header gets set correctly here.
Now for our login process in the API I need to use a custom media type (an extension of the application/json → application/something.v1+json) and I am unable to send this in the request.
I tried putting breakpoints in my API and the request reaches my required endpoint but since the “Content-Type” header is missing the request returns a 415 response code.
The httpProtocols have been kept basically the same as in the example file:
HttpProtocolBuilder httpProtocol =
http.baseUrl("https://computer-database.gatling.io")
.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
.acceptLanguageHeader("en-US,en;q=0.5")
.acceptEncodingHeader("gzip, deflate")
.header("Content-Length", "5000000")
.userAgentHeader(
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/119.0"
);
Am I doing something wrong or missing a setting somewhere?
So what I need is a POST request with a JSON body and a custom “Content-Type” header.