# Plugin API - Response headers are not transient

**URL:** https://discuss.elastic.co/t/plugin-api-response-headers-are-not-transient/173314
**Category:** Elasticsearch
**Created:** [March 21, 2019, 1:17pm UTC](https://discuss.elastic.co/t/plugin-api-response-headers-are-not-transient/173314 "2019-03-21T13:17:00Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Simone\_Scarduzio](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simone_scarduzio/32/17128_2.png) [@Simone\_Scarduzio](https://discuss.elastic.co/u/Simone_Scarduzio)
#### Post date: [March 21, 2019, 1:17pm UTC](https://discuss.elastic.co/t/plugin-api-response-headers-are-not-transient/173314/1 "2019-03-21T13:17:00Z")

</div>

From my Elasticsearch plugin, I want to set HTTP response headers. What I do is:

```java
threadPool.getThreadContext().addResponseHeader("header1","value1");

```

And this works, but when another request comes along, Elasticsearch will still carry "header1" as a response header. In other words, I expected the response headers to be transient, i.e. to be reset for every request.

Or at least, I expected to be able to be able clear/remove/overwrite them, but this is not possible as for current API.

```auto
// Throws Method threw 'java.lang.UnsupportedOperationException' exception.
threadPool.getThreadContext().getResponseHeaders().clear();

```

Please advise, am I doing this wrong?

---

<div class="post-metadata">

### Author: ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)
#### Post date: [March 29, 2019, 4:45pm UTC](https://discuss.elastic.co/t/plugin-api-response-headers-are-not-transient/173314/2 "2019-03-29T16:45:54Z")

</div>

Can you show more of what your plugin is doing? In which thread are you trying to modify the thread context? What plugin apis is your plugin extending?

---

<div class="post-metadata">

### Author: ![Simone\_Scarduzio](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simone_scarduzio/32/17128_2.png) [@Simone\_Scarduzio](https://discuss.elastic.co/u/Simone_Scarduzio)
#### Post date: [March 30, 2019, 1:02am UTC](https://discuss.elastic.co/t/plugin-api-response-headers-are-not-transient/173314/3 "2019-03-30T01:02:50Z")

</div>

all this went back to normal when I made sure the response headers were set from a thread in the original ES thread pool. Setting them from a completable future of an async HTTP client request caused problems.

Also, to make it work with 6.7.0 I had to wrap all my code in a:

```auto
try (ThreadContext.StoredContext ctx = threadPool.getThreadContext().stashContext()) {
  ... all my code that uses HTTP client and set response headers...
}

```

The above was necessary because the deprecation logger set a response header with a null value and threw a NullPointerException.

```auto
[2019-03-29T13:10:53,149][WARN][r.suppressed] [d-qW6Ay] path: /_template/metricbeat-6.7.0, params: {name=metricbeat-6.7.0}
java.lang.NullPointerException: value
	at io.netty.util.internal.ObjectUtil.checkNotNull(ObjectUtil.java:33) ~[?:?]
	at io.netty.handler.codec.DefaultHeaders.addObject(DefaultHeaders.java:327) ~[?:?]
	at io.netty.handler.codec.http.DefaultHttpHeaders.add(DefaultHttpHeaders.java:117) ~[?:?]
	at org.elasticsearch.http.netty4.Netty4HttpChannel.setHeaderField(Netty4HttpChannel.java:168) ~[?:?]
	at org.elasticsearch.http.netty4.Netty4HttpChannel.setHeaderField(Netty4HttpChannel.java:163) ~[?:?]
	at org.elasticsearch.http.netty4.Netty4HttpChannel.addCustomHeaders(Netty4HttpChannel.java:189) ~[?:?]
	at org.elasticsearch.http.netty4.Netty4HttpChannel.sendResponse(Netty4HttpChannel.java:113) ~[?:?]
	at org.elasticsearch.rest.RestController$ResourceHandlingHttpChannel.sendResponse(RestController.java:497) ~[elasticsearch-6.7.0.jar:6.7.0]
	at org.elasticsearch.rest.action.RestResponseListener.processResponse(RestResponseListener.java:37) ~[elasticsearch-6.7.0.jar:6.7.0]
	at org.elasticsearch.rest.action.RestActionListener.onResponse(RestActionListener.java:47) [elasticsearch-6.7.0.jar:6.7.0]
	at org.elasticsearch.action.support.TransportAction$1.onResponse(TransportAction.java:85) [elasticsearch-6.7.0.jar:6.7.0]
	at org.elasticsearch.action.support.TransportAction$1.onResponse(TransportAction.java:81) [elasticsearch-6.7.0.jar:6.7.0]

```

---

<div class="post-metadata">

### Author: ![jasontedor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasontedor/32/66992_2.png) [@jasontedor](https://discuss.elastic.co/u/jasontedor)
#### Post date: [March 30, 2019, 9:35pm UTC](https://discuss.elastic.co/t/plugin-api-response-headers-are-not-transient/173314/4 "2019-03-30T21:35:33Z")

</div>

> [@Simone\_Scarduzio](#):
>
> The above was necessary because the deprecation logger set a response header with a null value and threw a NullPointerException.

Can you share more details about this please?

---

<div class="post-metadata">

### Author: ![Simone\_Scarduzio](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simone_scarduzio/32/17128_2.png) [@Simone\_Scarduzio](https://discuss.elastic.co/u/Simone_Scarduzio)
#### Post date: [March 31, 2019, 12:58pm UTC](https://discuss.elastic.co/t/plugin-api-response-headers-are-not-transient/173314/5 "2019-03-31T12:58:20Z")

</div>

@jasontedor the issue was described by a user of trying to make the code work with ES 6.7.0

> <https://github.com/sscarduzio/elasticsearch-readonlyrest-plugin/issues/419>

Which was fixed with this commit:

> <https://github.com/sscarduzio/elasticsearch-readonlyrest-plugin/commit/cc584f4d3c8b96deb7429df39c9c6d709139da34>

---

<div class="post-metadata">

### Author: ![jasontedor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasontedor/32/66992_2.png) [@jasontedor](https://discuss.elastic.co/u/jasontedor)
#### Post date: [March 31, 2019, 2:15pm UTC](https://discuss.elastic.co/t/plugin-api-response-headers-are-not-transient/173314/6 "2019-03-31T14:15:37Z")

</div>

Okay, thanks for clarifying. The way that I read this is that it is not a problem in Elasticsearch server. Please correct me if I have it wrong.

---

<div class="post-metadata">

### Author: ![Simone\_Scarduzio](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simone_scarduzio/32/17128_2.png) [@Simone\_Scarduzio](https://discuss.elastic.co/u/Simone_Scarduzio)
#### Post date: [March 31, 2019, 3:22pm UTC](https://discuss.elastic.co/t/plugin-api-response-headers-are-not-transient/173314/7 "2019-03-31T15:22:01Z")

</div>

The main issue seems the extra null value in a certain response header which is not set by the plugin.

```auto
{Warning=[299 Elasticsearch-6.7.0-8453f77 "[types removal] The parameter include_type_name should be explicitly specified in put template requests to prepare for 7.0. In 7.0 include_type_name will default to 'false', and requests are expected to omit the type name in mapping definitions.", null]

```

Apparently the extra null is present only when the plugin is installed. Stashing the thread context removes the header entirely, but it does not look like a solution to the root cause.

Did we forget to implement some interface method?

---

<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: [April 28, 2019, 3:22pm UTC](https://discuss.elastic.co/t/plugin-api-response-headers-are-not-transient/173314/8 "2019-04-28T15:22:07Z")

</div>

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