Search code examples
scalapathakkauri

Uri.Path is not parsing / in scala


I have a filePath which I am trying to extract from a String. The string looks like:

val myPath: String = "InitialIndexValue/this/is/some/kind/of/path"
val firstIndex = myPath.indexOd('/')
val extractedPath = myPath.substring(firstIndex)
//I am creating a Uri path
val uriPath = Uri.Path./(extractedPath)

This is returning %2F in place of all '/'. The uri path is returning %2Fthis%2Fis%2Fsome%2Fkind%2Fof%2Fpath. The imports are:

import akka.http.scaladsl.model.Uri
import java.nio.file.Path

My questions are: Why '/' is relaced by "%2F" by Uri.Path? Is there any other way to handle this? TIA


Solution

  • Method / on Uri.Path is intended to concatenate path segments together into one path. The argument to / is a segment (e.g. "this" or "is" or "some"), not a whole path. When the segment contains special characters, they are URL encoded, e.g. character '/' becomes "%2F" and space would be "%20".

    What you probably need to use is the apply method of Uri.Path which parses the given string as a whole path:

    val uriPath = Uri.Path(extractedPath)