akka-http: How to set response headers
Asked Answered
C

1

10

I've a route as follows:

val route = {
    logRequestResult("user-service") {
      pathPrefix("user") {
        get {
          respondWithHeader(RawHeader("Content-Type", "application/json")) {
            parameters("firstName".?, "lastName".?).as(Name) { name =>
              findUserByName(name) match {
                case Left(users) => complete(users)
                case Right(error) => complete(error)
              }
            }
          }
        } ~
          (put & entity(as[User])) { user =>
            complete(Created -> s"Hello ${user.firstName} ${user.lastName}")
          } ~
          (post & entity(as[User])) { user =>
            complete(s"Hello ${user.firstName} ${user.lastName}")
          } ~
          (delete & path(Segment)) { userId =>
            complete(s"Hello $userId")
          }
      }
    }
  }

The content type of my response should always be application/json as I've it set for the get request. However, what I'm getting in my tests is text/plain. How do I set the content type correctly in the response?

On a side note, the akka-http documentation is one of the most worthless piece of garbage I've ever seen. Almost every link to example code is broken and their explanations merely state the obvious. Javadoc has no code example and I couldn't find their codebase on Github so learning from their unit tests is also out of the question.

Cosetta answered 7/9, 2015 at 4:18 Comment(2)
I agree with you on the state of the docs. I will say though that as a community project, individuals are free to take the initiative to help improve them.Geaghan
@Geaghan Individuals can only be expected to contribute once they've a good grasp of the code, which is severely hindered by the existing, poor, documentation. Yes, one can always look in the source code, but, again, that is not for beginners, time-consuming and shouldn't be necessary for most issues. If they always wanted people to look into the source code, they shouldn't pretend to have any documentation.Cosetta
C
9

I found this one post that says "In spray/akka-http some headers are treated specially". Apparently, content type is one of those and hence can't be set as in my code above. One has to instead create an HttpEntity with the desired content type and response body. With that knowledge, when I changed the get directive as follows, it worked.

import akka.http.scaladsl.model.HttpEntity
import akka.http.scaladsl.model.MediaTypes.`application/json`

get {
  parameters("firstName".?, "lastName".?).as(Name) { name =>
    findUserByName(name) match {
      case Left(users) => complete(users)
      case Right(error) => complete(error._1, HttpEntity(`application/json`, error._2))
    }
  }
}
Cosetta answered 7/9, 2015 at 4:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.