0

I am writing an app for SmartThings (www.smartthings.com) in their own IDE. I have an input field here that is supposed to be text input. I ask for a departure address:

section("Departing From:"){
    input "departFrom", "text", title: "Address?"
}

when putting in the value of Monterey, CA the value magically gets changed to a JSON Array with the values of [Monterey, CA]

I want to pass this value to an httpGET statement but I need to URLencode it first to omit spaces, etc.I have tried URLencoder with no success due to the JSON array.

I have tried join(",") with no luck as it adds double quotes to the value.

How can I get a clean Monterey%2C%20CA URL encoded value from this variable?

** bear in mind someone could input any combination of numbers, spaces, and commas into this input as an address. The mapquest API I am sending it to can handle all these things as long as they dont have special characters and spaces are URL encoded.

Jeff Beck
  • 3,944
  • 3
  • 28
  • 45
Brian
  • 622
  • 10
  • 29

2 Answers2

0

Maybe try:

def l = ['Monterey', 'CA']
assert URLEncoder.encode(l.join(', ')).replaceAll('\\+','%20') == 'Monterey%2C%20CA'

When it comes to replacing + sign, please see here

Community
  • 1
  • 1
Opal
  • 81,889
  • 28
  • 189
  • 210
-1

There are different types of URL encoding, but in this case there are two: One that converts spaces to %20 and one that converts spaces to +.

For the first, you'd use UriUtils:

def yourEncodedString = UriUtils.encodeUri(yourString.toString(), "UTF-8")

For the second, you'd use UrlEncoder:

def yourEncodedString = URLEncoder.encode(yourString.toString(), "UTF-8")

Alternatively (I think) you can use URLEncoder with UTF-16 to get what you want.

I've never had a fun time with UriUtils, so hopefully UrlEncoder will work for you.

paranoid
  • 415
  • 2
  • 5
  • as mentioned in above comments, URLencoder doesnt work because the input is an array not a string. The `tostring()` did nothing to improve. Result is `%5B%22felton%22%2C%22+ca%22%5D`. `uriutils` failed in complier `java.lang.NullPointerException: Cannot invoke method encodeUri() on null object` – Brian Jul 16 '14 at 22:41
  • First, make sure you have your tests coded correctly: `def town = "felton, ca" def encodedTown = URLEncoder.encode(town, "UTF-8") println encodedTown felton%2C+ca` You should make sure that you understand **why** you need %20 instead of +. From the javadocs: This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format. And from the HTML specification: application/x-www-form-urlencoded Forms submitted with this content type must be encoded as follows: Control names and values are escaped. Space characters are replaced by '+' – paranoid Jul 17 '14 at 14:28
  • If you're still convinced you need the %20, you can do this: `println URLEncoder.encode("felton, ca", "UTF-8").replace("+", "%20")` ` – paranoid Jul 17 '14 at 14:30
  • Perhaps I have been unclear but as stated in my OP and comment below it, my problem is dealing with the array, not with whether its `%20` or `+`. I get `%5B%22felton%22%2C%22+ca%22%5D` which is encoding the brackets and quotes. – Brian Jul 17 '14 at 22:07