# Multiple hosts in one Java Rest Client with different API Keys

**URL:** https://discuss.elastic.co/t/multiple-hosts-in-one-java-rest-client-with-different-api-keys/349069
**Category:** Elastic Security
**Created:** [December 11, 2023, 4:27pm UTC](https://discuss.elastic.co/t/multiple-hosts-in-one-java-rest-client-with-different-api-keys/349069 "2023-12-11T16:27:18Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![aliaksei\_dev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aliaksei_dev/32/129616_2.png) [@aliaksei\_dev](https://discuss.elastic.co/u/aliaksei_dev)
#### Post date: [December 11, 2023, 4:27pm UTC](https://discuss.elastic.co/t/multiple-hosts-in-one-java-rest-client-with-different-api-keys/349069/1 "2023-12-11T16:27:18Z")

</div>

Hi, having trouble with implementing Java Rest Client v.7.17 - the requirement is to use multiple hosts with one client. At the same time our elastic uses Api Keys with header "Authorization" to authorize. So my question is: is it somehow possible to set different headers for different hosts? I already tried interceptor - but it gives only relative URI (eg "/book/create") without host on which I should decide what headers set to use (our every host is fully operational and interchangeable)

---

<div class="post-metadata">

### Author: ![aliaksei\_dev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aliaksei_dev/32/129616_2.png) [@aliaksei\_dev](https://discuss.elastic.co/u/aliaksei_dev)
#### Post date: [December 12, 2023, 1:33pm UTC](https://discuss.elastic.co/t/multiple-hosts-in-one-java-rest-client-with-different-api-keys/349069/2 "2023-12-12T13:33:04Z")

</div>

Found solution. There is interceptor called RequestTargetHost. Host is stored here :

```auto
final HttpCoreContext coreContext = HttpCoreContext.adapt(context);
String host = coreContext.getTargetHost().getHostName();

```

---

<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: [January 9, 2024, 1:33pm UTC](https://discuss.elastic.co/t/multiple-hosts-in-one-java-rest-client-with-different-api-keys/349069/3 "2024-01-09T13:33:10Z")

</div>

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