# Extract url from response body and save it

**URL:** https://community.gatling.io/t/extract-url-from-response-body-and-save-it/1190
**Category:** Gatling (Open-Source)
**Created:** [July 18, 2014, 11:55am UTC](https://community.gatling.io/t/extract-url-from-response-body-and-save-it/1190 "2014-07-18T11:55:05Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Andrei\_Proskurin](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@Andrei\_Proskurin](https://community.gatling.io/u/Andrei_Proskurin)
#### Post date: [July 18, 2014, 11:55am UTC](https://community.gatling.io/t/extract-url-from-response-body-and-save-it/1190/1 "2014-07-18T11:55:05Z")

</div>

My intention is to catch the url from response body and put it into the next request. For example I have the following request:  
.exec(http(“goHome”).get("/"))

In the response I have the following piece:

```auto
<a href="./?1-1.Some_URL" wicket:id="addButton">Add something new</a>

```

What my request should be to save “./?1-1.Some\_URL” as ${myUrl} in case "wicked:id=“addButton”?

---

<div class="post-metadata">

### Author: ![Andrei\_Proskurin](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@Andrei\_Proskurin](https://community.gatling.io/u/Andrei_Proskurin)
#### Post date: [July 18, 2014, 11:55am UTC](https://community.gatling.io/t/extract-url-from-response-body-and-save-it/1190/2 "2014-07-18T11:55:44Z")

</div>

P.S. I know that I need to use regex but I need some assist.

---

<div class="post-metadata">

### Author: ![Andrei\_Proskurin](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@Andrei\_Proskurin](https://community.gatling.io/u/Andrei_Proskurin)
#### Post date: [July 21, 2014, 7:12am UTC](https://community.gatling.io/t/extract-url-from-response-body-and-save-it/1190/3 "2014-07-21T07:12:05Z")

</div>

I tried the following function (I would also like to check if fetched url is the one I need):  
val scenarioAddDeclarations = scenario(“addNew”)  
.exec(http(“HomePage”).get("/")  
.check(bodyString(  
“”“Lisa uus deklaratsioon”"").saveAs(“myUrl”)))  
.pause(2)  
.exec((session:Session) =\> {  
val myUrl = session.getAttributeAsOptionString  
if(myUrl.isDefined){  
println(myUrl.get)  
}  
session  
})

But I get the error:

found : String("\<a href=\* wicket:id=“addNewKmd”\>Lisa uus deklaratsioon")  
required: io.gatling.core.session.Session

---

<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: [July 21, 2014, 7:54am UTC](https://community.gatling.io/t/extract-url-from-response-body-and-save-it/1190/4 "2014-07-21T07:54:51Z")

</div>

You’re regex is wrong. You want a capture group. And a \* all by itself won’t work as expected.

You should:

- read more about Java patterns: [http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html](http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html)
- fix your pattern with an online tester before trying with Gatling: [http://java-regex-tester.appspot.com](http://java-regex-tester.appspot.com)

---

<div class="post-metadata">

### Author: ![Andrei\_Proskurin](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@Andrei\_Proskurin](https://community.gatling.io/u/Andrei_Proskurin)
#### Post date: [July 21, 2014, 8:14am UTC](https://community.gatling.io/t/extract-url-from-response-body-and-save-it/1190/5 "2014-07-21T08:14:14Z")

</div>

Hi,

I followed your advice and used the online tester (great tool btw, thanks very much!). It seems that correct regex for fetching my url would be:  
Lisa uus deklaratsioon

What about the other part of the code? Is the syntax correct?
