Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return a file from src/main/resources using akka-http

I have a file abc.txt in my project's resources folder src/main/resources. I have the following code snippet which is supposed to be serving the file.

get {
    path("hello") {
      complete {
        val s = Source.file(new File("src/main/resources/abc.txt"), 1024)
        s
      }
    }

I even tried the following

get {
path("hello") {
  complete {
    getFromFile("src/main/resources/abc.txt")
  }
}

In both the cases I get an error saying

Expression of type routing.Route doesn't conform to expected Type toResponseMarshallable

My only requirement is that when I get this request I should be able to serve a file. I did google for solution I couldn't find any. Note: I am a Scala/Akka beginner

like image 278
Alwin Doss Avatar asked Mar 21 '26 05:03

Alwin Doss


1 Answers

getFromFile is a Directive, while complete expects a response.

Correct solution should be like:

val route: Route =
  path("hello") {
    getFromResource("abc.txt")
  }
like image 69
Viktor Klang Avatar answered Mar 24 '26 02:03

Viktor Klang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!