Charset issue

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Charset issue

Odde
Hi

There seems to be a problem when setting the charset with the >\ method when used in combination with the << method. The request factory charset is used instead of the one specified. The program below demonstrates the problem:

object CharsetIssue {
  import dispatch.{Http, :/, Request}
  def main(args: Array[String]) {
    var value = "räksmörgås" // Swedish for shrimp sandwich
    var charset = "ISO-8859-1"
    var http = new Http
    val PostedData = "(?s).*text=([^<]+)</pre>.*".r
    val PostedData(result1) = http(:/("xformstest.org") / "cgi-bin/echo.sh" << Map(("text", value)) >\ charset >- { _.toString })
    println("Should be equal: " + java.net.URLDecoder.decode(result1, charset) + " == " + value)
    // If the factory charset is changed, it works
    changeFactoryCharset(charset)
    val PostedData(result2) = http(:/("xformstest.org") / "cgi-bin/echo.sh" << Map(("text", value)) >\ charset >- { _.toString })
    println("Should be equal: " + java.net.URLDecoder.decode(result2, charset) + " == " + value)
  }
  def changeFactoryCharset(charset: String) {
    val requestClass: Class[_] = Class.forName(Request.getClass.getName)
    val factoryCharsetField = requestClass.getDeclaredField("factoryCharset")
    var instance = requestClass.getField("MODULE$").get(requestClass)
    factoryCharsetField.setAccessible(true)
    val previousCharset = factoryCharsetField.get(instance)
    factoryCharsetField.set(instance, charset)
    println("Factory charset changed from " + previousCharset + " to " + charset)
  }
}

Best regards
Odd Möller
Reply | Threaded
Open this post in threaded view
|

Re: Charset issue

n8han
Administrator
There seems to be a problem when setting the charset with the >\ method when used in combination with the << method. The request factory charset is used instead of the one specified. The program below demonstrates the problem:

Hi, sorry it's not behaving that way currently. I added >\ for response handling and didn't think about using it for posts and puts, but it makes perfect sense. I've checked in the change to master and it will be in 0.7.2.

Nathan