gatling + how to send post with json payload?

171 views
Skip to first unread message

vijayakumar eswaran

unread,
Jan 31, 2020, 9:00:11 AM1/31/20
to Gatling User Group
hi all,

I am looking for inputs on sending POST http request with json as a request payload. if anyone has come across this use case or worked already, please do share the sample script.

Thanks,
Vijay

vijayakumar eswaran

unread,
Jan 31, 2020, 9:22:38 AM1/31/20
to Gatling User Group
I have been trying the below (captured from the online sources), but it end up in error.

.post("/platform/spi/job/v2/post")
.bodyPart(RawFileBodyPart("jobjson","data/job.json").contentType("application/json").fileName("job.json")).asJson


<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache Server at www.xxx.xxxx Port 443</address>
</body></html>

Stéphane LANDELLE

unread,
Jan 31, 2020, 9:23:44 AM1/31/20
to gat...@googlegroups.com
Pretty sure you're confusing a simple body and multipart (what you currently do).

Logo Stéphane Landelle
Chief Technical Officer
twitter: @slandelle
site:
gatling.io




--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gatling/687f80f1-d39c-4dcd-89f0-666e2b9583c8%40googlegroups.com.

Stéphane LANDELLE

unread,
Jan 31, 2020, 9:25:22 AM1/31/20
to gat...@googlegroups.com


Logo Stéphane Landelle
Chief Technical Officer
twitter: @slandelle
site:
gatling.io



vijayakumar eswaran

unread,
Jan 31, 2020, 9:52:55 AM1/31/20
to Gatling User Group
I would like to post a http request with json request body fed in.  
To unsubscribe from this group and stop receiving emails from it, send an email to gat...@googlegroups.com.

vijayakumar eswaran

unread,
Jan 31, 2020, 10:57:02 AM1/31/20
to Gatling User Group
thanks for the video link and I went through and tried below. end result is same. I am pretty sure, not doing the right thing here. any help would be highly appreciated.


.exec(http("postAJob")
.post("/platform/spi/job/v2/post")
.body(StringBody("""{"jobType":"R","jobStartDate":"2020-01-31","jobEndDate":"","attr-job_startDate_flexible":false,"schedule":[{"id":"sunday","times":[{"start":"08:00","end":"17:00"}]},{"id":"tuesday","times":[{"start":"08:00","end":"17:00"}]}],"attr-job_varyingSchedule":true,"jobZip":"02451","kids":[{"ccAgeGroups":"CHLDAGEGP004"}],"attr-job_lightHousekeeping":true,"attr-job_mealPreparation":false,"attr-job_laundry":false,"attr-job_homeworkHelp":false,"attr-job_errandsOrGroceries":false,"attr-job_comfortableWithPets":true,"collegeDegree":false,"attr-job_firstAidOrCPR":false,"attr-job_dropPickupChildren":false,"attr-job_own_transportation":false,"attr-job_nonSmoker":false,"jobRate":{"lowerValue":"12","higherValue":"17"},"serviceId":"CHILDCARE"}"""))
.headers(headers_2)
.check(bodyString.saveAs("jobresponse"))
)

Stéphane LANDELLE

unread,
Jan 31, 2020, 10:58:15 AM1/31/20
to gat...@googlegroups.com
Missing Content-Type request header maybe?


Logo Stéphane Landelle
Chief Technical Officer
twitter: @slandelle
site:
gatling.io



To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gatling/017b6f8d-0e48-4a98-abd0-c8bcb172948d%40googlegroups.com.

vijayakumar eswaran

unread,
Jan 31, 2020, 11:26:33 AM1/31/20
to Gatling User Group
exactly, thanks a lot for your quick response and help! much appreciated.

vijayakumar eswaran

unread,
Jan 31, 2020, 11:32:31 AM1/31/20
to Gatling User Group
quick question:

is there any example that I can refer to pass the json file at run time through json feeder and not hard code it in the main script?

Thanks,
Vijay

George Kandalepas

unread,
Jan 31, 2020, 11:40:30 AM1/31/20
to Gatling User Group
Hey Vijay,

you can try this:
.post("idm/api/v1/someservice")
.headers(sessionHeaders)
.body(ElFileBody("bodies/someservice_body.json")).asJson
.check(bodyString.saveAs("BODY"))

and in the body directory you will have your json payload:

someservice_body.json
{
"firstName": "George",
"displayName": "_george_",
"userDescription": "performance engineer"
}

and you can randomize these fields with a random feeder

-George

George Kandalepas

unread,
Jan 31, 2020, 12:04:43 PM1/31/20
to Gatling User Group
To add to my previous post you can randomize your json and use as a body as follows:

createUsers.scala
import scala.util.Random

import io.gatling.core.Predef._
import io.gatling.core.structure.ChainBuilder
import io.gatling.http.Predef._

object createUsers extends Simulation{
  
  val rndNumb = (10000 to 100000000).iterator

  def randomString(length: Int):String = {
rnd.alphanumeric.filter(_.isLetter).take(length).mkString
}

  val custRandUserFeeder: Iterator[Map[String, String]] = Iterator.continually(Map(
"mediumRandString" -> randomString( 15 ),
"smallRandString" -> randomString(5),
"longRandString" -> randomString( 200 ),
    "randomUserPrefix" -> ("George" + rndNumb.next()),
))

  def createNewUser: ChainBuilder = {
feed(custRandUserFeeder)
.exec(http("Create New User")
.post("idm/api/v1/createuser")
.headers(sessionHeaders)
.body(ElFileBody("bodies/createUserTemplate.json")).asJson
.check(bodyString.saveAs("BODY"))
.check(jsonPath("$['name']").exists.saveAs("newName"))
)
}
}

user_body.json
{
"firstName": "${randomUserPrefix}${smallRandomString}",
"lastName": "Kand${smallRandString}",
"roles": [
{
"name": "CONSUMER"
}
],
"locale": "en_US",
"userId": "gkandale${mediumRandString}",
"email": "gkandale${mediumRandString}@acme.com",
  "description": "${longRandString}",
  "pwdPolicyname": "strict_auth",
"status": "true"
}

Please excuse any typos!!
-GeorgeK

vijayakumar eswaran

unread,
Jan 31, 2020, 12:16:26 PM1/31/20
to Gatling User Group
great, precisely what I was looking for. will give it a try and get back.

thank you GeorgeK!
  "email": "gkandale${mediumRa...@acme.com",

George Kandalepas

unread,
Jan 31, 2020, 12:28:12 PM1/31/20
to Gatling User Group
BTW, the bodies directory should be in the resources directory. By default Gatling looks in resources dir to find the these types of files.

I hope it works out for you!

-George

vijayakumar eswaran

unread,
Feb 3, 2020, 3:15:12 AM2/3/20
to Gatling User Group
thanks a lot George, it worked! appreciate your help in this regard.

George Kandalepas

unread,
Feb 3, 2020, 11:14:06 AM2/3/20
to gat...@googlegroups.com
I'm glad i could help!!

To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gatling/d73ad001-a257-4320-8a93-de2c4037294d%40googlegroups.com.


--
George Kandalepas

George Kandalepas

unread,
Feb 3, 2020, 11:15:11 AM2/3/20
to Gatling User Group
Glad I could help Vijay!

tech stuff

unread,
Apr 6, 2020, 9:07:15 AM4/6/20
to gat...@googlegroups.com
I  do have a same scenario of sending json requests from resources to my scala file.One issue is,does it work with https requests also?.Help me on sending json data fles to a HTTPS requests.

To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gatling/1433d167-b662-45d3-9b32-104e7e7731b0%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages