# Elastic APM Java agent capture response body for Spring Boot application

**URL:** <https://discuss.elastic.co/t/elastic-apm-java-agent-capture-response-body-for-spring-boot-application/264237>\
**Category:** APM\
**Tags:** java\
**Created:** [February 14, 2021, 8:02am UTC](https://discuss.elastic.co/t/elastic-apm-java-agent-capture-response-body-for-spring-boot-application/264237 "2021-02-14T08:02:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ali\_Nazemian](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ali\_Nazemian](https://discuss.elastic.co/u/Ali_Nazemian)\
**Post date:** [February 14, 2021, 8:02am UTC](https://discuss.elastic.co/t/elastic-apm-java-agent-capture-response-body-for-spring-boot-application/264237/1 "2021-02-14T08:02:49Z")

</div>

I was wondering how I can capture a response body for a Spring Boot application using Elastic APM java agent? Is there any way that I can configure the agent to capture all responses or possibly the response object for a particular endpoint? I can confirm that request body has been captured by using the "transaction" option for APM config, but I cannot find any property for response body.

---

<div class="post-metadata">

**Author:** ![ylasri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ylasri/32/86120_2.png) [@ylasri](https://discuss.elastic.co/u/ylasri)\
**Post date:** [February 14, 2021, 8:28am UTC](https://discuss.elastic.co/t/elastic-apm-java-agent-capture-response-body-for-spring-boot-application/264237/2 "2021-02-14T08:28:39Z")

</div>

Not natively supported by a setup, check some links here.  
You can attach response body to the transaction manually by calling `apm.setCustomContext()`  
do it before the request have ended.

```auto
apm.setCustomContext({
  response: response_body
})

```

> **[Agent API | APM Node.js Agent Reference \[3.x\] | Elastic](https://www.elastic.co/guide/en/apm/agent/nodejs/current/agent-api.html#apm-set-custom-context)**

> <https://github.com/elastic/apm/issues/52>
>
> Description of the issue
> Currently, our agents offer the capture\_body configuration option which allows to capture the request body in transaction.context.request.body. I...

> **[HTTP configuration options | APM Java Agent Reference \[1.x\] | Elastic](https://www.elastic.co/guide/en/apm/agent/java/current/config-http.html#config-capture-body-content-types)**

---

<div class="post-metadata">

**Author:** ![Ali\_Nazemian](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ali\_Nazemian](https://discuss.elastic.co/u/Ali_Nazemian)\
**Post date:** [February 14, 2021, 1:51pm UTC](https://discuss.elastic.co/t/elastic-apm-java-agent-capture-response-body-for-spring-boot-application/264237/3 "2021-02-14T13:51:52Z")

</div>

Thank you for your help. I'm not sure if I understand what you meant by using setCustomContext(), do you happen to have any examples for Java agent where I can have a look?

---

<div class="post-metadata">

**Author:** ![nugusbayevkk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nugusbayevkk/32/126683_2.png) [@nugusbayevkk](https://discuss.elastic.co/u/nugusbayevkk)\
**Post date:** [February 14, 2021, 3:19pm UTC](https://discuss.elastic.co/t/elastic-apm-java-agent-capture-response-body-for-spring-boot-application/264237/4 "2021-02-14T15:19:29Z")

</div>

> [@ylasri](#):
>
> the transaction m

Hi @Ali_Nazemian ,  
you can capture REQUESTs body only. How to enable this, look into [capture\_body](https://www.elastic.co/guide/en/apm/agent/java/current/config-core.html#config-capture-body) and [capture\_body\_content\_types](https://www.elastic.co/guide/en/apm/agent/java/current/config-http.html#config-capture-body-content-types).  
For example, add vm argument: -Delastic.apm.capture\_body=ALL when running application  
and in `http.request.body.original` you can view your request body:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/d/9/d9fc029b801895c8e4f92911380818cefe971d69.png) .

I think that for performance reasons, the RESPONSE body capturing functionality has not been added.

For capture response body you need add `public-api` [dependency](https://www.elastic.co/guide/en/apm/agent/java/current/public-api.html):  
And in the required place of your application add custom context to transaction:

```auto
ElasticApm.currentTransaction().addCustomContext("custom.context.transaction.response_body", "string representation");
        

```

And in `discovery` tab, you can view your RESPONSE body data:  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/d/6/d66972944553dec776bd929c461f80cdc82a2408.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 7, 2021, 11:20am UTC](https://discuss.elastic.co/t/elastic-apm-java-agent-capture-response-body-for-spring-boot-application/264237/5 "2021-03-07T11:20:21Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
