create comma-separated queryParam from List[String] in session

From call A I extract a list of IDs (using jsonPath(“//id”).findAll.saveAs(“IDs”)). This works well, and I can do a foreach over these values. However, I now also want to do something like:

.queryParam(“foo”, session.getTypedAttributeList[String].mkString(“,”))

I want the resulting param to look like:

/foo/bar?IDs=12,4523,345

but I haven’t been able to make that work. Is it actually possible? If so, how?

Stig

You almost there, but you have to pass a function (here, you just have an instruction where session is not defined):

.queryParam(“foo”, (session: Session) => session.getTypedAttributeList[String].mkString(","))

or (type inferred)
.queryParam(“foo”, session => session.getTypedAttributeList[String].mkString(","))

or (wild card)
.queryParam(“foo”, _.getTypedAttributeList[String].mkString(","))

Cheers,

Stéphane

Thanks! I forgot to mention that I’m using Gatling2, so the method has changed a bit. But this works:

.queryParam(“foo”, session => session.getList[String].get.mkString(","))

Stig

But this works:

.queryParam("foo", session =>
session.get[List[String]]("IDs").get.mkString(","))

Or simply .queryParam("foo",_.get[List[String]]("IDs").get.mkString(","))

My bad, actually, you didn’t write it the proper way.

I mean, the syntax is correct and it will do the job, but not efficiently: it will generate an Exception if the attribute is absent from the Session (generally meaning that the request that was supposed to store it failed).

In Gatling 2, in order to handle such cases properly, we introduced Validations: https://github.com/excilys/gatling/wiki/Gatling-2#wiki-validation

The proper way is :

queryParam(“foo”, .getVList[String].map(.mkString(","))

  • getV returns a Validation that contains either the attribute, or an error message (not an exception) if the attribute was absent, or if it wasn’t of the expected type

  • map converts the content if the Validation was a Success

  • the signature of the second param is no longer Session => String like in Gatling 1, but Session => Validation[String] (we alias this as Expression[String])
    Get it?

PS: What you wrote compiled because your String was implicitly converted into a Validation[String]