writing values extracted from http response to a csv file in gatling

HI,

I am new to gatling and have a requirement where I have to save the values extracted from one of the my http response to the csv file. How I can do that. I am able to extract the values and print without any issue.

here is the code snippet:

def CaptureCCid() = {
 exec(
    http("CaptureCCid")
  .get(baseTokenUrl + "/oauth2/v0/authorize?response_type=token&redirect_uri="+baseUrl+"%2Fo2c.html&realm=raas&client_id=78c3881c-3b65-4b81-a4c0-624d41d0cfce&scope=travelrequest.write&state=JWT_implicit")
  .check(status.is(200),
   regex("""name="cc" value="([^"]*)""").saveAs("correlationId"))

 )
}

HI,

I was able to resolve the problem by using Java io libraries below is the code snippet which is working for me.

Libraries to import:

import java.io.File
import java.io.PrintWriter
import java.io.FileOutputStream
def requestCreate() :ChainBuilder = {
  exec(
    http("requestCreate")
      .post("/v4/requests")
      .body(StringBody(requestBody)).asJSON
      .headers(headerTravel)
      .check(status.is(201),
        regex(""""id":"((\\"|[^"])*)"""").saveAs("Id"))
  )
  .exec { session =>
   val s1 = new File("gatling/src/test/resources/data/Test1.csv")
    if(s1.exists()){
      val writer = new PrintWriter(new FileOutputStream(new File("gatling/src/test/resources/data/Test1.csv"), true))
      writer.write(session("Id").as[String].trim)
      writer.write("\n")
      writer.close()
    }
    else {
      val writer = new PrintWriter(new FileOutputStream(new File("gatling/src/test/resources/data/Test1.csv"), true))
      writer.println("RequestID")
      writer.write(session("Id").as[String].trim)
      writer.write("\n")
      writer.close()
    }
    session
  }

Great bro

Hey Navdeep,
I was trying your solution and I was wondering if you could explain to me how does this code line looks for the created file:

val writer = new PrintWriter(new FileOutputStream(new File("data/tokens.csv"), true))

You see, I’ve tried changing the path (“src/resources/data/tokens.csv”, "“gatling/resources/data/tokens.csv”, “…/src/resources/data/tokens.csv”) but I can’t make it writing in the “tokens.csv” created file. I always get the same error:

java.io.FileNotFoundException: data\tokens.csv (El sistema no puede encontrar la ruta especificada)

The script creates the file, but It does not write in it.
I’m new at Gatling and I’ve been here for days :frowning:

I would appreciate any help.

Hi All,

I also tried to use the same code for extratimg value from response.But its creating a file.But not writing the data.
I have imported the libraries mentioned

@melissa QA
Even im facing same issue, any solution you found for this.