How to save json response in new csv file or how to save response is public variable

Dear Gatling Team,

Help please …
How to save json response in new csv file or how to save response is public variable so that i can use for next API to pass as request paramater(API chaning)

So for i tried with my code:

package dealerUserService_userInfo

import hyke_BaseConfig.BaseSimulation
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
import io.gatling.core.structure.ChainBuilder

class fetch_user_info extends BaseSimulation {

//define feeder
val csvfeeder = csv(fileName = “data/UserLoginwithEmail.csv”)

//Define Request
def User_login_with_emial_method(): ChainBuilder = {
feed(csvfeeder)
.exec(http(requestName = “User Login with Email”)
.post(url = “/dealer/user/v1/login”)
.body(StringBody(
“”"

{“emailId”: “${emailId}” ,“password”: “${password}”}

“”".stripMargin)).asJson
.check(jsonPath(path = “$.data.auth.token”).saveAs(key = “numberIDs”))
.check(bodyString.saveAs(“RESPONSE_DATA”)))
.exec(session => {
println(“Some Restful Service Response Body:”)
println(session(“RESPONSE_DATA”).as[String])
session
})
//Save variable numberIDs into the csv file
.foreach("${numberIDs}", “id”) {
exec(session => {
numberIDs_writer.println(session(“id”).as[String])
session
})
}

}

//Variables to store the extracted values of the response
var numberIDs : Seq[String] = _

//Java Writer
val numberIDs_writer = {
val fos = new java.io.FileOutputStream(“data/numbersIDs.csv”)
new java.io.PrintWriter(fos,true)
}

//setup scenario

val scn = scenario(scenarioName = “Trying to generate OTP for Mobile registered Users”)
.exec(User_login_with_emial_method())

//inject User -Load profile
setUp(scn.inject(atOnceUsers(users = 1))
//rampusers(500) over (30 minutes).maxDuration(3600 seconds)
.protocols(httpConf))
}

Error message :

22:07:51.099 [ERROR] i.g.a.Gatling$ - Run crashed
java.io.FileNotFoundException: data/numbersIDs.csv (No such file or directory)

Note : but i have used same path for CSV feeder for data driven its working file.

Please please can you guide me how to save response in new CSV file or in Public variable how to store values in public variable.

Hi Guys i found solution for How to save Response in CSV
!! Hopefully these will help Someone

package DUS.Login

import java.io.{File, FileOutputStream, PrintWriter}

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

class Login_withMobile extends BaseSimulation {

//define feeder
val csvfeeder = csv(fileName = “data/UserLoginwithMobile.csv”) // fetching data from CSV file to run data driven testing

//Define Request
def User_login_with_Mobile_method(): ChainBuilder = {
feed(csvfeeder)
.exec(http(requestName = “User Login with Mobile”)
.post(url = “/dealer-user-service/v1/login”)
.body(StringBody(
“”"

{“mobileNumber”: “${mobileNumber}” ,“code”: “${code}”,“password”: “${password}”}

“”".stripMargin)).asJson

.check(jsonPath(path = “$.data.auth.token”).saveAs(key = “token”)) //getting path of token from response using json path finder
.check(jsonPath(path = “$.data.user.uid”).saveAs(key = “user_id”))
)
.pause(2)
.exec { session =>
println(“this is token”)
println(session(“token”).as[String])
val writer = new PrintWriter(new FileOutputStream(new File(“src/test/resources/data/EmailLoginToken.csv”), false)) // Im saving generated token to EmailLoginToken.csv
writer.write(“Token_generated,userID”)
writer.write("\n")
writer.write(session(“token”).as[String])
writer.write(",")
writer.write(session(“user_id”).as[String])
writer.write("\n")
writer.close()
session

}
}

//setup scenario

val scn = scenario(scenarioName = “Trying to Login User with Mobile”)
.exec(User_login_with_Mobile_method())

//inject User -Load profile
setUp(scn.inject(atOnceUsers(users = 1))
//rampusers(500) over (30 minutes).maxDuration(3600 seconds)
.protocols(httpConf))

}