Home > Software engineering >  Uri.Path is not parsing / in scala
Uri.Path is not parsing / in scala

Time:12-05

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 / in place of all '/'. The uri path is returning /this/is/some/kind/of/path. The imports are:

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

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

CodePudding user response:

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 "/" and space would be " ".

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)
  • Related