0

I am creating a web service with play framework and scala. The service get a url and a language, and use this information, but i can't put a url into the service.

I have this in routes GET /trans/$url<(http%3A%2F%2Fa)>/:lang controllers.Application.getTranslation(url:String, lang:String)

and in Application: def getTranslation(iri: String,lang:String) = Action { implicit request => etc.

and i want to receive something like '/trans/htp://a/es' (its http instead of htp but i can't write it here)

is it posible? Because i've tried everything and i'm not able. is ther ane way to pass a url as parameter?

Mankx
  • 13
  • 2

1 Answers1

1

You have to url escape and unescape on both sides. It is called URL Encoding or Percent Encoding (because escaped character are rewritten using a combination that start with %) https://en.wikipedia.org/wiki/Percent-encoding

Here is a question that give you information if your client side is in js: Encode URL in JavaScript?

And here is nice scala library that can be used to encode and decode URL persent encoding. https://github.com/theon/scala-uri

I have started scala myself (most of my prod work on playframework is still in Java), so I do not know if there is a better scala uri library out there.

EDIT : You got me curious so I searched a bit: http://www.motobit.com/util/url-encoder.asp you should be carefull about the encoding on both sides.

Community
  • 1
  • 1
le-doude
  • 3,345
  • 2
  • 25
  • 55
  • Ok thank you, with this i can decode and encode the URL, but i still have a problem. In the routes file, how can i pass a url like: http://localhost:9000/trans/'http://hello.com'/es I have the routes definited: GET /trans/:iri/:lang controllers.Application.getTranslation(url:String, lang:String) But it only works if y pass the url with encoding %... – Mankx Apr 15 '13 at 17:39
  • well the string you will receive as the url param of your getTranslation(url, lang) will be escaped, then you have to unescape it within the function before using it. I am not entirely what is your issue here? Maybe do an edit of your question with a bit of code example and your use case? That would help me ... help you? – le-doude Apr 16 '13 at 09:06