# Script urlEncode example?

**URL:** <https://forum.inductiveautomation.com/t/script-urlencode-example/69947>\
**Category:** Ignition\
**Created:** [February 16, 2023, 3:58pm UTC](https://forum.inductiveautomation.com/t/script-urlencode-example/69947 "2023-02-16T15:58:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 16, 2023, 3:58pm UTC](https://forum.inductiveautomation.com/t/script-urlencode-example/69947/1 "2023-02-16T15:58:43Z")

</div>

My head is slightly fried today. Can anyone show me how to encode the URL:

```auto
client = system.net.httpClient()
machine = 3
sku = '123 45'
response = client.get("http://10.11.12.13/proxy/recipes.php?machine=%s&sku=%s" % (str(machine), str(sku)))

```

The space in `sku` is causing the problem.

Many thanks.

---

<div class="post-metadata">

**Author:** ![Matthew.gaitan](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Matthew.gaitan](https://forum.inductiveautomation.com/u/Matthew.gaitan)\
**Post date:** [February 16, 2023, 4:29pm UTC](https://forum.inductiveautomation.com/t/script-urlencode-example/69947/2 "2023-02-16T16:29:41Z")

</div>

did you try replacing the space with `%20`?

Supposedly you can do this too:

```auto
import urllib
params = {
	'machine': 3,
	'sku': '123 45'
}
baseUrl = 'http://10.11.12.13/proxy/recipes.php'
urlEncodedParams = urllib.urlencode(params)

url = "%s?%s" % (baseUrl, urlEncodedParams)

```

prints out `http://10.11.12.13/proxy/recipes.php?machine=3&sku=123+45 `

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 16, 2023, 4:36pm UTC](https://forum.inductiveautomation.com/t/script-urlencode-example/69947/3 "2023-02-16T16:36:11Z")

</div>

> [@Matthew.gaitan](#):
>
> did you try replacing the space with `%20`?

No, while that might work, I want to do it properly! 😉  
The `sku` value is passed into the script so I would have to urlEncode that before merging it with the rest of the URL.

* * *

Oh, I see you've added more. I'll try that now.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 16, 2023, 4:46pm UTC](https://forum.inductiveautomation.com/t/script-urlencode-example/69947/4 "2023-02-16T16:46:57Z")

</div>

I strongly recommend **not** using urllib. Instead use Java's standard [URLEncoder](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/URLEncoder.html) class. Consider using the method that takes a `Charset` object. See the list from [StandardCharsets](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/charset/StandardCharsets.html).

{ I recommend **not** using **any** jython libraries when java or the jars included in Ignition have an alternative. Better performance, fewer bugs, and no issues with python2's lack of encoding awareness. That is my definition of "properly". }

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [February 16, 2023, 4:53pm UTC](https://forum.inductiveautomation.com/t/script-urlencode-example/69947/5 "2023-02-16T16:53:44Z")

</div>

You don't need to do _any_ manual encoding. `system.net.httpClient` will do it for you.

```python
client = system.net.httpClient()
params = dict(machine=3, sku='123 45')
response = client.get("http://10.11.12.13/proxy/recipes.php", params=params)

```

Proof:

```python
from pprint import pprint

client = system.net.httpClient()
params = dict(machine=3, sku='123 45')
response = client.get("http://httpbin.org/get", params=params)

pprint(response.json)

```

```auto
>>> 
{'args': {'machine': u'3', 'sku': u'123 45'},
 'headers': {'Content-Length': u'0',
             'Host': u'httpbin.org',
             'Http2-Settings': u'AAEAAEAAAAIAAAABAAMAAABkAAQBAAAAAAUAAEAA',
             'Upgrade': u'h2c',
             'User-Agent': u'Ignition',
             'X-Amzn-Trace-Id': u'Root=1-63ee5f84-75acab695cd7841d1434ed14'},
 'origin': u'104.220.10.151',
 'url': u'http://httpbin.org/get?machine=3&sku=123+45'}

```
