2

I am pushing some variables to a httpGet statement and it is failing on any spaces, how can I replace the spaces in the variable with %20 prior to running the GET?


I am trying to encode "felton, ca" which is in the variable from with def fromFormatted = URLEncoder.encode(from, "UTF-8")

and when I run the code I get the error

groovy.lang.MissingMethodException: No signature of method: static java.net.URLEncoder.encode() is applicable for argument types: (org.codehaus.groovy.grails.web.json.JSONArray, java.lang.String) values: [[felton, ca], UTF-8]

original input field

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

Also tried def fromFormatted = from.encodeAsURL()

with the result of

%5B%22felton%22%2C%22ca%22%5D


apparently since the input text contained a , then it automatically treated that input text as an array. Any thoughts on keeping this from happening, or converting it to a string and maintaining the comma?


I resolved the array by using def fromStr = from.join(",")

unforunately join creates a json string that adds double quotes

%22felton%22%2C%22ca%22

Is there another method for joining the array that would maintain the comma without adding quotes?

Brian
  • 622
  • 10
  • 29
  • You need to url encode the string. Check http://stackoverflow.com/questions/5330104/encoding-url-query-parameters-in-java for details – suman j Jul 16 '14 at 16:09
  • I tried `def fromFormatted = URLEncoder.encode(from, "UTF-8")` and get error `groovy.lang.MissingMethodException: No signature of method: static java.net.URLEncoder.encode() is applicable for argument types: (org.codehaus.groovy.grails.web.json.JSONArray, java.lang.String) values: [[felton, ca], UTF-8]` – Brian Jul 16 '14 at 16:51
  • add code to the questions. Its difficult to guess otherwise. Based on the error, It looks like "from" variable is a `List` and not a string. – suman j Jul 16 '14 at 16:52
  • You are passing a `JSONArray` where `URLEncoder.encode()` requires a `String`. – evanwong Jul 16 '14 at 17:06
  • strange..considering the input field is just `text`. how can i convert this field to a plain string? – Brian Jul 16 '14 at 17:10
  • The `from` field does not look like it is coming from a text field as part of normal form submit. You have something else in the mix here that will explain why `from` is a JSONArray. – Jeff Scott Brown Jul 16 '14 at 17:13
  • Even if your call to encode worked, I think you are going to get a `+` and not `%20` in place of the space. The same thing you would get from calling `from.encodeAsURL()`. – Jeff Scott Brown Jul 16 '14 at 17:14
  • ya i tried `from.encodeAsURL()` and it encoded the brackets and quotes.`%5B%22felton%22%2C%22ca%22%5D` I need to convert this array to a string somehow. – Brian Jul 16 '14 at 17:17
  • Can you add the actual implementation to the question what is being tried yet? – dmahapatro Jul 16 '14 at 17:56

0 Answers0