Attribute issue

Hi folks. I’m new to Gatling and to Scala and trying to execute some scenarios. I have got an issue with my scenario when I try to use “token” in my request body - No attribute named ‘token’ is defined"

can any one figure out what is wrong or what is missing to define?
.exec(session => session.set(“token”, token)) =>Tried to use this expression but still gives me error “Value token: not found”

val scn = scenario(“scenario name”)
.exec(http(“get_token”)
.post("/oauth/token")
.body(StringBody("""{“grant_type”: “pass”, “client_id”: “12345678990”, “client_secret”: “secretkey”, “username”: “xxcdcd”, “password”: “xxx”, “scope”: “”}""")).asJSON
.check(status.is(200))
.check(jsonPath(".access_token").find.saveAs(“token”))

)
.pause(1)
.exec(http(“get_Authorization”)
.post("/profile")
.body(StringBody("""{“access_token”: “${token}”, “application-key”: “33bdndfnnfnrnt556677”}""")).asJSON
.check(status.is(200))
.check(jsonPath(".Authorization_token").find.saveAs(“Authorization”))
)
.pause(2)
.exec(http(“Get_locations”)
.get("/api/locations?application-key=33ede4567890123e4e4")
.header(“Authorization”, “${token}”)
.check(status.is(200))

.check(jsonPath(".access_token").find.saveAs(“token”))

The root element of a jsonPath is “$”

Try .check(jsonPath("$.access_token").find.saveAs(“token”))

And I’d update all your other jsonPaths as well.

Hi,
Tried using the root element in all the jsonPath (.check(jsonPath("$.access_token").find.saveAs(“token”))) but still getting the same issue. The following are the headers used in the code. Is there any issue with headers?

package computerdatabase

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._

class BasicSimulationAll extends Simulation {

val httpConf = http
.baseURLs(“URL1” , “URL2”)

.acceptHeader(“text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8”)
.doNotTrackHeader(“1”)
.acceptLanguageHeader(“en-US,en;q=0.5”)
.acceptEncodingHeader(“gzip, deflate”)
.userAgentHeader(“Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0”)

val headers_10 = Map(“Content-Type” → “”“application/x-www-form-urlencoded”"")
val headers_9 = Map(“Content-Type” → “”“application/json”"")
val headers_8 = Map(“access_token” → “”“token”"")

Hm, I don’t think this would cause the problem but you don’t need the find in your check.

Try adding this after your first exec block that executes the request:

.exec(session => {
println(session(“token”).asOption[String])
session
})

that will print Some(xyz) or None based on if the token is actually saved in the session or not. I’d expect the check to fail if it didn’t actually find any token, since it implicitly calls exists.

As per suggestion, Removed find in my check and added the below .exec function after the first exec block but still the same. Attachment is the error screenshot please check.

Attribute error.PNG

That doesn’t show the console output I mentioned. What does

.exec(session => {
println(session(“token”).asOption[String])
session
})

produce?

Out put of the below execution result shows found nothing. screenshot attached

.exec(session => {
println(session(“token”).asOption[String])
session
})

What you’re posting is inconsistent.

The found nothing error is what happens if the response does not contain anything at the specified path. Your previous post doesn’t seem to indicate that.

.exec(session => {
println(session(“token”).asOption[String])
session
})

does not actually affect the request/response at all, it is simply a debug statement which will print whatever is stored as “token” in the session. If you get that found nothing error, that means there’s no token to save in the response.

Hi Andrew,

I think I got to fix first this first. In actual am not able to print the token. When executing the below simple code for receiving the token get the error "jsonPath($.token).find(0).exists, found nothing".
is there something from the request point am missing to declare and get the token in response?

val scn = scenario(“Bearer token”)
.exec(http(“request_1”)
.post("/oauth/token")
.body(StringBody("""{ “grant_type”: “password”, “client_id”: “client1xxxxxxx”, “client_secret”: “13cxxxxx”, “username”: “user”, “password”: “xxxx”, “scope”: “”}""")).asJSON
.check(jsonPath("$.access_token").saveAs(“token”))
)
.exec(session => {
println(session(“token”).asOption[String])
session
})

It sounds like your JsonPath is not extracting right results. Why not test your json repsonse and json path to remove that possibility

http://jsonpath.curiousconcept.com/ - choose steffan goesener implementation.

My Json response seems to be fine, As requested tried with goesener implementation by adding Json data and Json path expression ($.access_token) then processing it, got the access_token out put result.
I have just added the header ( .header(“access_token”, “${token}”) ) to my code then i dont see the error ( "jsonPath($.token).find(0).exists, found nothing" ) but still experiencing the issue with “No Attribute named token is defined”. From code point i dont see that anything is missed**. Do we have to define token as val?**

Please share your simultation and JSON response. Otherwise it’s all a guess work.