# How can I extract the data from the response only if the response content not empty.

**URL:** <https://community.gatling.io/t/how-can-i-extract-the-data-from-the-response-only-if-the-response-content-not-empty/1557>\
**Category:** Gatling (Open-Source)\
**Created:** [October 17, 2014, 7:37am UTC](https://community.gatling.io/t/how-can-i-extract-the-data-from-the-response-only-if-the-response-content-not-empty/1557 "2014-10-17T07:37:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dinesh\_Reddy](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@Dinesh\_Reddy](https://community.gatling.io/u/Dinesh_Reddy)\
**Post date:** [October 17, 2014, 7:37am UTC](https://community.gatling.io/t/how-can-i-extract-the-data-from-the-response-only-if-the-response-content-not-empty/1557/1 "2014-10-17T07:37:20Z")

</div>

Hi,

I am trying to write a http request and capture some information from the response. But in some cases even though http status code is 200, response will be empty (content length is 0). I am trying to figure out how to extract the data using regex, only if the response content is available i.e content length is greater than 0.

Following is my current request.

val execDeviceHB = exec(http(“Device Ping For Policies”)  
.put("${tenantname}%sdevicemanager/ota/server".format(context))  
.body(StringBody(Session =\> getIdleReq()))  
.check(status.is(200))  
.check(header(“Content-Length”).not(“0”))  
.check(regex("""([\w]+[:][\d]+)""").saveAs(“devicepolicypush”))  
.check(regex("""(\w+):""").saveAs(“commandtype”))

BTW, how do I define the doIfEqualsOrElse condition? Can you please provide an example?

Thanks in advance,  
Regards

Dinesh

---

<div class="post-metadata">

**Author:** ![jarrowwx](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@jarrowwx](https://community.gatling.io/u/jarrowwx)\
**Post date:** [October 17, 2014, 2:41pm UTC](https://community.gatling.io/t/how-can-i-extract-the-data-from-the-response-only-if-the-response-content-not-empty/1557/2 "2014-10-17T14:41:01Z")

</div>

Easy solution is to just extract the whole body into a session variable, then in the next step do a session function to parse it. Then you have the advantage of full scala capabilities to do logic.

---

<div class="post-metadata">

**Author:** ![Carlos\_Torres1](https://avatars.discourse-cdn.com/v4/letter/c/47e85d/32.png) [@Carlos\_Torres1](https://community.gatling.io/u/Carlos_Torres1)\
**Post date:** [October 17, 2014, 4:54pm UTC](https://community.gatling.io/t/how-can-i-extract-the-data-from-the-response-only-if-the-response-content-not-empty/1557/3 "2014-10-17T16:54:41Z")

</div>

You could use something like follows:  
`  
…  
.check(regex("""…""").optional.saveAs(“devicepolicypush”))  
…

`

With optional, saveAs doesn’t fail if the response is empty, but if later you want to read the value from the session you’ll need to check if the value is set.

For example

`doIfEqualsOrElse(session => session("devicepolicypush").asOption[String].exists(_.trim.nonEmpty)) { // Equals chain } { // Else chain }`

Note: I haven’t tried running this code, it is just a suggestion.

I hope that helps.  
Carlos

---

<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:** [October 17, 2014, 5:15pm UTC](https://community.gatling.io/t/how-can-i-extract-the-data-from-the-response-only-if-the-response-content-not-empty/1557/4 "2014-10-17T17:15:42Z")

</div>

Note that Gatling EL now has a .exists() function, so you could write doIfEqualsOrElse(“devicepolicypush.exists()”)

---

<div class="post-metadata">

**Author:** ![Carlos\_Torres1](https://avatars.discourse-cdn.com/v4/letter/c/47e85d/32.png) [@Carlos\_Torres1](https://community.gatling.io/u/Carlos_Torres1)\
**Post date:** [October 17, 2014, 5:23pm UTC](https://community.gatling.io/t/how-can-i-extract-the-data-from-the-response-only-if-the-response-content-not-empty/1557/5 "2014-10-17T17:23:08Z")

</div>

Nice, wasn’t aware of that one. Thanks Stephane.

---

<div class="post-metadata">

**Author:** ![Meena\_Dappili](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@Meena\_Dappili](https://community.gatling.io/u/Meena_Dappili)\
**Post date:** [July 18, 2019, 9:46am UTC](https://community.gatling.io/t/how-can-i-extract-the-data-from-the-response-only-if-the-response-content-not-empty/1557/6 "2019-07-18T09:46:30Z")

</div>

For one of my application is sending this response body on login request [once i login with user id and password] this token i supposed to capture and add to the headers. can any one help me on this?

{“user”:{“userSid”:17,“password”:null,“userLoginId”:“user1”,“userAssociatedId”:“user1”,“firstName”:“john”,“lastName”:“User”,“emailAddress”:“john\_user1@\*\*\*\*\*.com”,“lastLoginDate”:1563272556060,“languageProfileId”:2,“userFunctionGroupId”:10,“userGroupId”:3,

“token”:“eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NjMzMTU3NTUsInVzZXJfbmFtZSI6ImJyYW5kX3VzZXIxIiwiYXV0aG9yaXRpZXMiOlsiVlBTLUVYVEVOU0lPTiIsIlByb2R1Y3Rpb24tQ2hhbmdlIiwiRVJST1ItTE9HIiwiVXBkYXRlLVNoaXBtZW50IiwiQ0hBTkdFLU1HTVQiLCJpbXBvcnRmaWxlIiwiU01TLUZhYnJpYy1MaWJyYXJ5IiwiQ29tbWl0bWVudC1DcmVhdGlvbi1FZGl0IiwiTUFTVEVSLURBVEEiLCJtc3QtdGFzay11cGRhdGUiLCJQUk9ELU1HTVQiLCJRQVMiLCJJTlRFUk5BTC1URVNULVJFUE9SVCIsIlNPVVJDSU5HLVdGIiwiR0lTLVdPUktGTE9XLUUyRS0yIiwiRW1haWwtU3Vic2NyaXB0aW9uIiwiRVhQT1JULURBVEEiLCJDcmVhdGUtTmV3LVN0ZXAiLCJJTlNQRUNUSU9OLU1BTkFHRU1FTlQiLCJXb3JrLWluLVByb2dyZXNzLVRyYWNrZXIiLCJCQVRDSC1UUklHR0VSIiwiVVNFUi1TVUJTQ1JJUFRJT04iLCJSZXZpZXctcmVjZWlwdC1jaGFuZ2VzIiwiQ0hBTkdFX01HTVQiLCJDcmVhdGUtcmVjZWlwdC1jaGFuZ2UtcmVxdWVzdHMiLCJSRVNQT05TSVZFLU1BTkFHRU1FTlQiLCJBRE1JTiIsImdlbmVyYXRlcmVwb3J0IiwiQ2FwYWNpdHktQm9va2luZy1hbmQtUlBUIiwiRVhQT1JULURBVEEtUUEiLCJVdGlsaXphdGl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VzcG9uc2l2ZS1wbGFubmluZy1tYW5hZ2VtZW50IiwiVXRpbGl6YXRpb24\_dHlwZT1TZWFyY2giLCJyZXZpZXctY2FwYWNpdHkiLCJQUk9EX01HTVQiLCJDcmVhdGUtTmV3LUxETCIsIlByb2R1Y3Rpb24tUmVwb3J0aW5nIiwiYm9va2luZyIsImZsYWctb2ZmY3ljbGUtUE8iLCJSZXZpZXctRGV2LVNhbXBsZS1SZXF1ZXN0cyIsIlJlY2VpdmUtU2hpcG1lbnQiLCJjb21taXRtZW50LW1hbmFnZW1lbnQiLCJERVRBSUwiLCJDb21taXRtZW50P3R5cGU9QXBwcm92YWwiLCJUZW1wbGF0ZS1Db25maWciLCJNaWxlc3RvbmUtQ29uZmlnIiwiV0lQIiwic2NtcyIsIlJldmlldy1BRC1TYW1wbGUtUmVxdWVzdHMiLCJCT09LSU5HLU1HTVQiLCJDb21taXRtZW50LU1hbmFnZW1lbnQiLCJHSVMtV09SS0ZMT1ctUUEiXSwianRpIjoiMzM5NjcwNjktN2MzMS00NTRhLTk1MzEtYzlhNjAwNTVhMTlhIiwiY2xpZW50X2lkIjoiZ2F0ZXdheSIsInNjb3BlIjpbInJlYWQiLCJ3cml0ZSJdfQ.aEVJzlkcsR4I3cyKdSuD9rwywMIBjGk5my6MhC-y3bNMqSh4By1Yzyu3vaHScqnOzG5d593Zz07kme3awKi7LlGLlGpDa95hX3GdtCjRUNQmW3hf3vljxbrZXMq1Aj0wR-O0AC6wjQxR9GlwQ6IinI-49pRnuUDzbu5PIeF-7AzYfNYcRvuhHjYcgZ\_cx6yAYGNDuJrLW0CHrirR8hSx9bcVuproz0wEcB7P3BUbp5nltteeQrEEx7DrUX\_4MhsTEf0\_LVJArXuoR5MITt5h7OHgRnYXbAsOMCidJpuNdwLVLpsJuaimmBbme6qmkH2Gh0l9wGXYWHQzNbUvXzFQkQ”,“accessGroupList”:[{“accessGroupSid”:18,“accessGroup”:“SERVICE\_REQUEST\_EDIT”,“accessGroupDescription”:"SERVICE REQUEST - MST …}
