Retrieve headers from the response

Good afternoon,

Is it possible to retrieve headers from the http response ?

I didn’t found it in the github wiki and API docs.

My use case is to automatically retrieve the ETag hash of a resource to send it in the next request (to have the 304/not change and mimic the client’s behavior).

Thank you,

Hi Raphaël,

You should be able to retrieve headers thanks to the headers checks : https://github.com/excilys/gatling/wiki/Checks#wiki-header and then use saveAs, as usual :wink:

Cheers,
Romain

Thank you. Here is an example :

.exec(
http(“main css”)
.get("/assets/stylesheets/doc/main.css")
.headers(headers)
.check(header(“Etag”).saveAs(“main.css-Etag”))
)
.pause(1)
.exec(
http(“main css 304”)
.get("/assets/stylesheets/doc/main.css")
.header((“If-None-Match”, “${main.css-Etag}”))
.check(status.is(304))
)
.pause(1)

The check succeeded, so I guess it works.

You can sort of “debug” your scenario if you want to.

Just use a simple action :

.exec(
(s:Session) => println(s.getAttribute(“myKey”)); s
)

It’ll show the captured value.

Note that this should be used only in “dev mode”.

Cheers,
Romain

Hi,
I tried to do a println like this but have a compilation error with 1.2.5 :

/usr/local/gatling/user-files/simulations/staging/CookieCache250.scala:41: error: type mismatch;
found : com.excilys.ebi.gatling.core.Predef.Session => Unit
required: com.excilys.ebi.gatling.core.action.builder.ActionBuilder
(s:Session) => println(s.getAttribute(“aisakosSegs”))
^

My aim is just to check a cookie, It seems to me that the simplest way is to get the value from session as it said here : https://github.com/excilys/gatling/wiki/HTTP that cookies are stored in the session

First, your simulation deson’t compile because you’re missing the ; s in the example below. As the Session class is immutable and one might want to edit it in a Session function, Session functions have to return a Session.

Then, cookies are stored in a CookieStore which is store in the Session, they are note store directly as attributes. The storage key for the CookieStore is “gatling.http.cookies”.
You cant directly print the CookieStore, it has a user-friendly toString method.

Cheers,

Steph

2012/8/9 usul <fdussert@gmail.com>

Hi !
The return of the header :slight_smile:

It seems that gatling can’t parse my cookie properly.
I have this in my scenario :

val scn = scenario(“Users Pool 2”)
.exec(
http(“Get marker UP2”)
.get("/marker/"))
.feed(recordsInformation)
.exec(
http(“Get image UP2”)
.get("/marker/image/perf/?cref=")
.headers(headers ++ Map(“Cookie” → “aisakosId=${BID}”, “User-Agent” → “Gatling/5.0 UP2 ${BID} ${SEGMENTS}”))
.check(status.is(200),
headerRegex(“Set-cookie”, (session: Session) => “aisakosSegs=” + session.getTypedAttributeString + “.*”)))

As you could see on http://pastebin.com/UxvXrK9F
My feeder is used as it does, there’s the good values in the cookie and in the user-agent.
I’ve put those values in user-agent just for check.

And as you see it said : Check ‘exists’ failed, found None
But there’s the set cookie header :confused:

Perhaps because there’s some + in my cookie value ?

Cheers

François

Hi François,

I’ll have a look tomorrow.

BTW, you could simply write:
headerRegex(“Set-cookie”, “aisakosSegs=${SEGMENTS}.*”)

Cheers,

Stéphane

2012/9/3 usul <fdussert@gmail.com>

Seems to me that I’ve alerady tested with ${} and it was not working, I’ll try again :wink:

I have same result with headerRegex(“Set-cookie”, “aisakosSegs=${SEGMENTS}.*”)
Cheers

François

Sure, that’s just a more simple way of writing the same thing.
The problem is exactly what you suspected: there’s some special characters in your expression that have a regex meaning.

You have to escape them:
headerRegex(“Set-cookie”, (session: Session) => “aisakosSegs=” + session.getTypedAttributeString.replace("+", “\+”) + “.*”)))

Cheers,

Steph

PS: When will you stick to simple things?! 328.png

2012/9/3 François Dussert <fdussert@gmail.com>

And again you have the solution !
Thanks a lot
Cheers

François

Given:

[1,2] client ← HTTP → server1
[3] client ← HTTP → server2

  1. client sends http request to get oauth token from server1
  2. server1 replies with oauth token in http header
  3. client sends http request to server2, with the oauth token (from server1) as a request header

Rather than check that the header is present, how can I use that header for my above work-flow?

Thanks

Why not save the header with saveAs so you can add it so the next requests?

I’m running into same issue and not able to understand how to retrieve response headers ie; X-RateLimit-Limit etc.

.check(header("Location").saveAs("authorizeRedirect"))