# Painless support for URI encoding

**URL:** https://discuss.elastic.co/t/painless-support-for-uri-encoding/108901
**Category:** Elasticsearch
**Created:** [November 23, 2017, 1:55pm UTC](https://discuss.elastic.co/t/painless-support-for-uri-encoding/108901 "2017-11-23T13:55:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Ryan\_Grannell](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ryan_grannell/32/41712_2.png) [@Ryan\_Grannell](https://discuss.elastic.co/u/Ryan_Grannell)
#### Post date: [November 23, 2017, 1:55pm UTC](https://discuss.elastic.co/t/painless-support-for-uri-encoding/108901/1 "2017-11-23T13:55:07Z")

</div>

Hi,

I wasn't sure where to post this, since it's specific to the painless language, but given that I'm using thing from Alerting I figured this was the appropriate category.

I'm trying to URLencode a string, which should be possible with the following code:

```
Base64.getUrlEncoder().encodeToString(myString.getBytes())

```

However, Painless does not seem to include the method `.getBytes()` on `java.lang.String`, which makes the `encodeToString` a bit useless. Is there any way to convert a string to a byte-array (the parameter type of `encodeToString`) in Painless.

Thanks,  
Ryan

---

<div class="post-metadata">

### Author: ![TimV](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timv/32/13162_2.png) [@TimV](https://discuss.elastic.co/u/TimV)
#### Post date: [November 23, 2017, 11:53pm UTC](https://discuss.elastic.co/t/painless-support-for-uri-encoding/108901/2 "2017-11-23T23:53:11Z")

</div>

> [@](#):
>
> I'm trying to URLencode a string, which should be possible with the following code:
> 
> ```auto
> Base64.getUrlEncoder().encodeToString(myString.getBytes())
> 
> ```

That does not produce a URL encoded string.  
`Base64.getUrlEncoder()` returns a URL-safe base64 encoder - that is, it is a Base64 encoder that only uses characters that are "url-safe". Specifically it uses `_` and `-` for the 2 non-alphnumeric characters and doesn't include newlines.  
That is not a URL encoder.

Unfortunately, I don't believe painless includes support for URL encoding.

---

<div class="post-metadata">

### Author: ![Ryan\_Grannell](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ryan_grannell/32/41712_2.png) [@Ryan\_Grannell](https://discuss.elastic.co/u/Ryan_Grannell)
#### Post date: [November 27, 2017, 1:52pm UTC](https://discuss.elastic.co/t/painless-support-for-uri-encoding/108901/3 "2017-11-27T13:52:00Z")

</div>

> [@TimV](#):
>
> Base64.getUrlEncoder() returns a URL-safe base64 encoder - that is, it is a Base64 encoder that only uses characters that are "url-safe". Specifically it uses \_ and - for the 2 non-alphnumeric characters and doesn't include newlines.

Hi Tim,

Thanks, I appreciate the clarification.

Regards,  
Ryan

---

<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: [December 25, 2017, 1:52pm UTC](https://discuss.elastic.co/t/painless-support-for-uri-encoding/108901/4 "2017-12-25T13:52:02Z")

</div>

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