# cannot read auth token from response header

**URL:** <https://community.gatling.io/t/cannot-read-auth-token-from-response-header/906>\
**Category:** Gatling (Open-Source)\
**Created:** [March 27, 2014, 11:43am UTC](https://community.gatling.io/t/cannot-read-auth-token-from-response-header/906 "2014-03-27T11:43:58Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anjali\_Wadhwa](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@Anjali\_Wadhwa](https://community.gatling.io/u/Anjali_Wadhwa)\
**Post date:** [March 27, 2014, 11:43am UTC](https://community.gatling.io/t/cannot-read-auth-token-from-response-header/906/1 "2014-03-27T11:43:58Z")

</div>

Hello,

I am using Gatling for first time.

In my application, I have a login page protected with CSRF protection as shown below:

> Login

For each display of this login the token will be different and it is generated dynamically.

To extract this token I tried following ways:

1. **regex:**

> `val scn = scenario("Login")`  
> `.exec(http("My website")`  
> `.get("/login")`  
> `.headers(headers_1)`  
> `.check(regex("""<meta content="(.*?)" name="csrf-token">""").saveAs("auth_token"))`  
> `.exec(http("Login")`  
> `.post("/login")`  
> `.headers(headers_5)`  
> `.param("csrf-token", "${auth_token}")`  
> `.param("username", "xxxx"))`

1. **header:**

.check(header(“tokenName”).saveAs(“token”)

1. **xpath:**

.check(xpath(“//head//meta[@name=‘csrf-token’]”))

1. **css:**

.check(css(“head meta[name=“csrf-token”]”))

None of the above can find the token and it gives error as “_**19:36:23.567 [WARN] c.e.e.g.h.a.GatlingAsyncHandlerActor - Request ‘My website’ failed : Check ‘exists’ failed, found None**_”

Am I missing something. Any thoughts?

Thanks,  
Anjali

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [March 27, 2014, 11:56am UTC](https://community.gatling.io/t/cannot-read-auth-token-from-response-header/906/2 "2014-03-27T11:56:31Z")

</div>

> 1. \*regex: \*
> 
> val scn = scenario("Login")
> 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.exec(http("My website")  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.get("/login")  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.headers(headers\_1)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.check(regex("""\<meta content="(.\*?)"  
> > name="csrf-token"\>""").saveAs("auth\_token"))  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.exec(http("Login")  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.post("/login")  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.headers(headers\_5)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.param("csrf-token", "${auth\_token}")  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.param("username", "xxxx"))

This should work, at least it does on my machine with latest snapshot.  
You'd better debug, there's a good chance the server doesn't send the page  
you expect.

> 2.\* header:\*

Won't work, this is for HTTP headers, you want to search in the body!

> 3. \*xpath:\*

I doubt your page is valid XHTML.

> 4. \*css:\*

I don't think css selectors can select meta tags.

---

<div class="post-metadata">

**Author:** ![Anjali\_Wadhwa](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@Anjali\_Wadhwa](https://community.gatling.io/u/Anjali_Wadhwa)\
**Post date:** [March 27, 2014, 2:47pm UTC](https://community.gatling.io/t/cannot-read-auth-token-from-response-header/906/3 "2014-03-27T14:47:40Z")

</div>

Hello,

Thanks for your reply.

I confirmed that my server returns me the page that I am expecting by applying some checks on page.

I see that check is failed only for tags and works fine for other tags.

I tried this on v1.5.4 and v2.0.0M3.

Thoughts?

Thanks,  
Anjali

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [March 27, 2014, 3:19pm UTC](https://community.gatling.io/t/cannot-read-auth-token-from-response-header/906/4 "2014-03-27T15:19:01Z")

</div>

Are you sure it doesn’t get populated with Ajax?

---

<div class="post-metadata">

**Author:** ![Yi\_Han](https://avatars.discourse-cdn.com/v4/letter/y/d07c76/32.png) [@Yi\_Han](https://community.gatling.io/u/Yi_Han)\
**Post date:** [March 28, 2014, 2:03am UTC](https://community.gatling.io/t/cannot-read-auth-token-from-response-header/906/5 "2014-03-28T02:03:08Z")

</div>

I also found this problem in my env, the csrf tag is rendered by rails, but seems that any ‘meta’ tag can not be caught by gatling actually.

In fact I tried JMeter and it looks fine, but they(with Gatling) are quite similar to each other in this part, that really confuses me.

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [March 28, 2014, 2:15am UTC](https://community.gatling.io/t/cannot-read-auth-token-from-response-header/906/6 "2014-03-28T02:15:40Z")

</div>

Could I get an export of this page, please?

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [March 28, 2014, 2:16am UTC](https://community.gatling.io/t/cannot-read-auth-token-from-response-header/906/7 "2014-03-28T02:16:42Z")

</div>

Also, could you try latest snapshot, please?  
[https://github.com/excilys/gatling/wiki/Continuous-Integration](https://github.com/excilys/gatling/wiki/Continuous-Integration)

---

<div class="post-metadata">

**Author:** ![Yi\_Han](https://avatars.discourse-cdn.com/v4/letter/y/d07c76/32.png) [@Yi\_Han](https://community.gatling.io/u/Yi_Han)\
**Post date:** [March 28, 2014, 9:11am UTC](https://community.gatling.io/t/cannot-read-auth-token-from-response-header/906/8 "2014-03-28T09:11:12Z")

</div>

Thanks for your help! I tried to print the export of the login page recieved by gatling, that line shows:

But what I got from browser(chrome) is Actually I also tried to use regex like this: """\<meta content="(.\*?)" name=""" Since there is already another line similar to this one, so may be it matches more than one results, but gatling reports 'non found' in this case, and that's what confused me a lot... Anyway it's resolved, Thanks for your help and I would like to find a better way to do debug, but before I think I have to learn scala firstly:p

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [March 28, 2014, 9:33am UTC](https://community.gatling.io/t/cannot-read-auth-token-from-response-header/906/9 "2014-03-28T09:33:54Z")

</div>

Gatling is not a browser, so it doesn't build the DOM nor execute  
Javascript.  
Gatling works on the HTTP protocol level, so what matters is what is on the  
wire, not in the browser memory.

As a consequence, you mustn't use the "page source" when designing your  
tests.  
The page source is your DOM, so it's how your browser parsed and  
interpreted the received HTML and how javascript modified the DOM.

You have to use tools such as Google Chrome Developer Tools or Firebox, and  
use what you get from the Network panel. Or use the Gatling logs.

Here, your problem is that the page your server sends is not valid HTML, so  
Chrome fixed it automatically.  
In HTML, self-closing is only valid for tags that can have content.  
\<meta\>foo\</meta\> is impossible, so neither are \<meta\>\</meta\> and \<meta /\>  
(those are valid XHTML, but not HTML).

Cheers,

Stéphane

---

<div class="post-metadata">

**Author:** ![Yi\_Han](https://avatars.discourse-cdn.com/v4/letter/y/d07c76/32.png) [@Yi\_Han](https://community.gatling.io/u/Yi_Han)\
**Post date:** [March 28, 2014, 9:45am UTC](https://community.gatling.io/t/cannot-read-auth-token-from-response-header/906/10 "2014-03-28T09:45:55Z")

</div>

Thanks for your explain:p  
I also found this problem because such csrf meta tag is built-in generated in rails like \<%= csrf\_meta\_tags %\> automatically, and other meta tags written manually never conclude ‘/’. Does it mean rails use wrong html syntax for tag generating?  
That taught me a lot, thanks again:p

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [March 28, 2014, 9:56am UTC](https://community.gatling.io/t/cannot-read-auth-token-from-response-header/906/11 "2014-03-28T09:56:14Z")

</div>

Have a look at the Content-Type HTTP header of the HTTP response that returns your page (from Dev Tool/Network).

If it says text/html, then you’re supposed to generate valid HTML, rails or your rails set up (I guess the rendering style can be configured somewhere) is wrong and your manual tags are right.

If it says application/xhtml+xml, then you’re supposed to generate valid XHTML and it’s the opposite.

---

<div class="post-metadata">

**Author:** ![Jothimani](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@Jothimani](https://community.gatling.io/u/Jothimani)\
**Post date:** [April 6, 2017, 12:57pm UTC](https://community.gatling.io/t/cannot-read-auth-token-from-response-header/906/12 "2017-04-06T12:57:18Z")

</div>

Hi,  
I am facing the same issue, is there any way to retrieve the token from DOM content?

Thanks,  
Jothimani

---

<div class="post-metadata">

**Author:** ![Mishra\_Sambit](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@Mishra\_Sambit](https://community.gatling.io/u/Mishra_Sambit)\
**Post date:** [April 7, 2017, 12:27am UTC](https://community.gatling.io/t/cannot-read-auth-token-from-response-header/906/13 "2017-04-07T00:27:10Z")

</div>

Looks like some of us are in the same boat — trying to get over the initial hump.

I like am new to Gatling, but trying to use it for my Play/Scala/Java environment. It will really help to put an example in the documentation (or may be in Quick Start guide) section.
