Gatling encode value twice

I have a successful encoding as this:

.exec(http("request_ticket")   
.post("https://xxx/")   
.header("Authorization", "Basic xxx")   
.headers(headers_access_token)   
.body(ElFileBody("bodies/ticket_request.json"))   
.check(regex("ticket\" : \"(.*?)\"").transform(rawTicketValue => 
java.net.URLEncoder.encode(rawTicketValue, "UTF-8")).saveAs("ticket")))

I want to encode it once more and wonder how to do it?

Something like this maybe?

.exec(session=>{   "${ticket}" = java.net.URLEncoder.encode("${ticket}", "UTF-8")   session})

The above states “illegal assignment target”

Hello Magnus!

If I am not mistaken, you can only use Gatling EL (i.e., “${ticket}”) in functions that accept Expression[...] type of parameters. Also, assigning to a session variable like "${ticket}" = ... is probably seen by Scala as trying to assign a value to a String literal hence the illegal assignment target error you see.

I am not an expert but would the following work in your case?

.exec { session => {
  val rawTicketValueEncodedOnce = session("ticket").as[String]
  val rawTicketValueEncodedTwice = java.net.URLEncoder.encode(rawTicketValueEncodedOnce, "UTF-8")
  session.set("ticket", rawTicketValueEncodedTwice)
}}

One more idea could be the following:

.exec(
  http("request_ticket")   
    .post("https://xxx/")   
    .header("Authorization", "Basic xxx")   
    .headers(headers_access_token)   
    .body(ElFileBody("bodies/ticket_request.json")
)   
.check(
  regex("ticket\" : \"(.*?)\"")
  .transform(rawTicketValue => 
    java.net.URLEncoder.encode(java.net.URLEncoder.encode(rawTicketValue, "UTF-8"), "UTF-8")
  )
  .saveAs("ticket")
)

(I might have messed up the parentheses above!)

Hope that helps a little! :slight_smile:

Hi gkallergis,
My problem is that I first need to encode the ticket once and use it in the next request, before I encode it once more and use it in the preceding request.

Then I would assume if I can do a “stand alone” exec just to encode twice it would be a good idea.

Alternatively I could encode the ticket the second time within the actual .exec that needs it double-encoded in the lines of this example:

.exec(http(“request_Mockinnlogging”)
.get(“https://xxx-webapp/”)
.headers( headers_3 )
.queryParam(“ticket” , (s:Session) => java.net.URLEncoder. encode (“”“${ticket}”“” , “UTF-8”))
.check(regex(“<INPUT TYPE="HIDDEN" NAME="jwt" VALUE="(.*?)"”).saveAs(“jwt”)))

where the queryparam here is the crucial part.

BTW: the above does not work as it gives me this url for the .exec:

https://xxx-webapp/&ticket=%2524%257Bticket%257D

Hello Magnus!

I think the problem with java.net.URLEncoder.encode("""${ticket}""", “UTF-8”)) is the same as I mentioned in my previous comment. The Java method encode() doesn’t know anything about Gatling’s EL. You are effectively encoding the String litteral ${ticket} here. If I undestand correctly, you can’t pass a session variable anywhere unless the method you are calling is expecting a paramter of type Expression[...].

Just to verify I understand correctly, what you want to do is the following:

  1. Fire a request and get a value from it, encode it and store it in the session.
  2. Fire another request including the encoded value from step #1.
  3. Encode the already encoded value and store it in the session.
  4. Fire another request including the twice-encoded value.

As you say in your initial post, steps #1 and #2 are working for you and you already have an encoded value in your session.

For step #3, did the exec block I provided in my previous comment work? If not, did you get any specific errors?

The call you provide in your last post could be altered this way to cover for both step #3 and #4:

.exec(http(“request_Mockinnlogging”)
  .get(“https://xxx-webapp/”)
  .headers( headers_3 )
  .queryParam(“ticket” , (s:Session) => java.net.URLEncoder. encode (s.("ticket").as[String] , “UTF-8”))
  .check(regex("<INPUT TYPE=“HIDDEN” NAME=“jwt” VALUE="(.*?)"").saveAs(“jwt”)))

I haven’t checked any of the above so treat it as pseudocode.

Yes it did, thank you for great help!

1 Like