Hello,
I’m new to this, so please bear with me.
I’m getting a pesky 500 call on a post from my gatling script that works fine when I use a normal rest client on my machine. Can any of you spot the issue? I appreciate that this is a vague question, but any help you can provide would be appreciated.
Here’s an edited version of my script…
val httpConf = http
.noProxyFor(“localhost”)
.acceptHeader("/")
.acceptCharsetHeader(“ISO-8859-1,utf-8;q=0.7,*;q=0.3”)
.acceptLanguageHeader(“en-GB,en-US;q=0.8,en;q=0.6”)
.acceptEncodingHeader(“gzip,deflate,sdch”)
.userAgentHeader(“Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36”)
.disableCaching
.disableWarmUp
val scn = scenario(“Web Service API Test”).during(1000 milliseconds) {
exec(http(“POST test”)
.post(“URL REMOVED FOR PRIVACY”)
.body(StringBody("""{“params” :[ { “id”: “31675801”, “paramType”: “INPUT”, “name”: “Input - Input DataId”, “type”: “int”, “value”: “240007”, “unit”: “”, “values”: “” }, { “id”: “31675803”, “paramType”: “PARAM”, “name”: “Input - Invert”, “type”: “bool”, “value”: 0, “values”: “”}"""))
.check(status.is(200)))
}
setUp(scn.inject(atOnceUsers(1)).protocols(httpConf))
Easy isn’t it?
Logback gives me the following output…
Request:
POST test: KO status.is(200), but actually found 500
You probably lack some request headers, like Content-Type, so that the server knows which kind of payload to expect?
Server logs would probably tell you.
I had wondered that too. On REST Console (a Chrome extension), the request headers are…
- Accept: /
- Connection: keep-alive
- Content-Type: application/xml
- Origin: chrome-extension: //rest-console-id
- User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
…and that’s all. Call you see anything in that list that isn’t supported in httpConf?
REST Console sends Content-Type: application/xml for a JSON payload, really???
But then, yes, this Content-Type header is missing.
I guess REST Console defaults to application/xml.
…and now the embarrassing question. Can you recommend the best way of setting the contentType in my example?
The example .headers() function on exec I found gives me compilation errors
“value headers is not a member of io.gatling.http.request.builder.Http”
Sorry for the probably obvious question.
http(“POST test”)
.post(“URL REMOVED FOR PRIVACY”)
.header(“Content-Type”, “application/json”)
. body…
Excellent, thank you. The header is now set, but I still get 500’s.
I had another idea. The Chrome client gives me 500’s if I pass an empty body. Does the body set up code look ok to you?
exec(http(“POST test”)
.post(VALID DEFINED URL HERE)
.header(“Content-Type”, “application/xml”)
.body(StringBody(“”“{“params” :[ { “id”: “31675801”, “paramType”: “INPUT”, “name”: “Input - Input DataId”, “type”: “int”, “value”: “240007”, “unit”: “”, “values”: “” }, { “id”: “31675803”, “paramType”: “PARAM”, “name”: “Input - Invert”, “type”: “bool”, “value”: 0, “values”: “”}”“”)).asJSON
.check(status.is(200)))
}
Logback shows it as…
stringData={“params” :[ { “id”: “31675801”, “paramType”: “INPUT”, “name”: “Input - Input DataId”, “type”: “int”, “value”: “240007”, “unit”: “”, “values”: “” }, { “id”: “31675803”, “paramType”: “PARAM”, “name”: “Input - Invert”, “type”: “bool”, “value”: 0, “values”: “”}
…rather than something that explicitly says body.
I missed the fact you had “.asJSON”, that does exactly the same thing as .header(“Content-Type”, “application/xml”) (so you can remove it), sorry.
Now, I realize what your problem is: your JSON payload is broken!
You don’t close the “params” array, there’s a ] missing at the very end.
Ha! Sometimes it is the least of quirks that causes the greatest strife! It is now working like a charm.
Thank you for your help and your patience.
You’re welcome.
Have fun!