Generating a MD5 hash of HTTP request body and storing it into a session attribute

1,473 views
Skip to first unread message

Andy Potter

unread,
Oct 21, 2016, 12:37:29 PM10/21/16
to Gatling User Group
After much searching online and reviewing the Gatling source code and docs I'm still stumped with how to generate a MD5 hash of the body of an HTTP request in order to then use it in a subsequent request. 

I know I can use a signature calculator to generate the MD5 checksum if I want to add it to the current request. But I couldn't work out how to store this value into the session in order to use it on a subsequent request.

In my test I basically need to upload a message body to one web service. And then send the MD5 checksum of the message along with other meta data to another web service. 

The snippet below shows the problem. The 'upload' steps put the message into a webservice, this is where I need to capture the MD5 checksum of the body so that it can be used in the subsequent call to the 'sendMsg' steps.

Any advice would be very gratefully received as this is blocking me from getting my load test operational.

val upload = exec(http("MsgSvcUpload")
.post("${msgSvcMsgUrl}")
.headers(Map(("X-Auth-Token", "${keystoneToken}"), ("accept", "*/*"), ("X-Delete-After", "86400")))
.body(ElFileBody("${fileBody}"))
.check(status.is(201))
)

val sendMsg = exec(http("MsgSvcSendMsg")
.post("${svcUrlMsgSvc}/v1.0/send/message")
.headers(Map(("X-Auth-Token", "${keystoneToken}"), ("accept", "application/json")))
.body(ElFileBody("msgSvcSendMsg.json")).asJSON
.check(status.is(200))
.check(header("content-type").is("application/json"))
)

Andy Potter

unread,
Oct 25, 2016, 4:21:46 AM10/25/16
to Gatling User Group
I finally found an acceptable solution to this problem. Although it does involve processing the file body twice which is not great but in my case the bodies are quite small so it's not a huge problem.

Here is the solution:

.exec(session => {
val bodyExpr = ElFileBody("${fileBody}")
val bodyStr = bodyExpr(session).get
session.set("msgSvcMsgMd5", digestMD5.digest(bodyStr.getBytes).map("%02x".format(_)).mkString)
.set("msgSvcMsgSize", bodyStr.length)
})

Eugene Abramchuk

unread,
Apr 6, 2018, 8:17:18 AM4/6/18
to Gatling User Group
Thanks Andy
Reply all
Reply to author
Forward
0 new messages