gatling + how to send post with json payload?

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

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
400 Bad Request

Bad Request

Your browser sent a request that this server could not understand.


Apache Server at www.xxx.xxxx Port 443

Pretty sure you’re confusing a simple body and multipart (what you currently do).

https://gatling.io/gatling-frontline/resources/#section-01

I would like to post a http request with json request body fed in.

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"))
)

Missing Content-Type request header maybe?

exactly, thanks a lot for your quick response and help! much appreciated.

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

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

To add to my previous post you can randomize your json and use as a body as follows:

createUsers.scala

great, precisely what I was looking for. will give it a try and get back.

thank you GeorgeK!

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

thanks a lot George, it worked! appreciate your help in this regard.

I’m glad i could help!!

Glad I could help Vijay!

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.