Send Json direct from db query as body of http request

Sorry if an answer for this exists but I cannot find one.
My script below works fine. It sends the contents of a json file in my http .body repeatedly (yes, the same contents, over and over again).
All is good.
However, I am manually creating that json file by:
1.querying the db in SSMS
2.taking the json output from the query and saving it to a .json file
3.quoting the json file name in the .body line of the script below.

I believe Gatling could do this for me in runtime and I have set about doing it.
I think my query is working but I don’t know how to take the json output and just send it in the body.
All the help I can find seems to assume you want to iterate through rows and columns in the database.
I imagine it will be quite a simple solution but it’s eluded me so far.
Thanks in advance :slight_smile:

`
package computerdatabase

import io.gatling.core.Predef._
import io.gatling.core.structure.ScenarioBuilder
import io.gatling.http.Predef._
import scala.concurrent.duration._
import io.gatling.http.protocol.HttpProtocolBuilder
import io.gatling.jdbc.Predef._

class DBUpdate extends Simulation {
// below is my connection and query that in SSMS returns json which works in the below myfile.json file
private val dbConnectionString = “jdbc:sqlserver://myserver;databaseName=mydb”
private val sqlQuery = “select top 1500 Col1, Col2, Col3 from dbo.[mytable] Where Type <>1004 AND SET is not null AND OPEN is not null For Json Path, Root(‘DatabaseUpdates’)”
private val sqlUserName = “mypass”
private val sqlPassword = “mypwd”

val sqlQueryFeeder = jdbcFeeder(dbConnectionString,
sqlUserName,
sqlPassword,
sqlQuery
)

val scn1 = scenario(“DBUpdate”)

.exec(
http(“DBUpdate”)
.put(“https://myurl”)
.header(“Execution-Context”,"{“Context”:[{“Key”:“CorrelationStack”,“Value”:“myguid”}, {“Key”:“PrincipalId”,“Value”:“myguid”}],“Stack”:[],}")
.header(“Ocp-Apim-Subscription-Key”, “mykey”)
.header(“Content-Type”, “application/json”)
.body (RawFileBody(“myfile.json”))
//how do I take that json output from the db query in Gatling and send it here as the .body?
.check(status.is(200))
)

setUp(scn9.inject(constantConcurrentUsers(1) during (10 minutes)))
}
`

This is exactly the purpose of the before { block } - See https://gatling.io/docs/current/general/simulation_structure/#hooks

The only catch is, you will have to use standard Java/Scala to obtain the JSON.

There is another solution that uses Gatling DSL, but it is complicated. I recommend you avoid it to keep your simulations readable.

Thanks John. Appreciate the response. When I’m back at the laptop I’ll have a dig into it. :slight_smile: