# Send Json direct from db query as body of http request

**URL:** <https://community.gatling.io/t/send-json-direct-from-db-query-as-body-of-http-request/5271>\
**Category:** Gatling (Open-Source)\
**Created:** [October 4, 2019, 2:46pm UTC](https://community.gatling.io/t/send-json-direct-from-db-query-as-body-of-http-request/5271 "2019-10-04T14:46:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SB1](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@SB1](https://community.gatling.io/u/SB1)\
**Post date:** [October 4, 2019, 2:46pm UTC](https://community.gatling.io/t/send-json-direct-from-db-query-as-body-of-http-request/5271/1 "2019-10-04T14:46:54Z")

</div>

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 🙂

`  
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](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)))  
}  
`

---

<div class="post-metadata">

**Author:** ![jarrowwx](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@jarrowwx](https://community.gatling.io/u/jarrowwx)\
**Post date:** [October 4, 2019, 8:37pm UTC](https://community.gatling.io/t/send-json-direct-from-db-query-as-body-of-http-request/5271/2 "2019-10-04T20:37:10Z")

</div>

This is exactly the purpose of the before { block } - See [https://gatling.io/docs/current/general/simulation\_structure/#hooks](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.

---

<div class="post-metadata">

**Author:** ![SB1](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@SB1](https://community.gatling.io/u/SB1)\
**Post date:** [October 5, 2019, 2:29pm UTC](https://community.gatling.io/t/send-json-direct-from-db-query-as-body-of-http-request/5271/3 "2019-10-05T14:29:30Z")

</div>

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