akka-http: How to set response headers

后端 未结 1 1036
温柔的废话
温柔的废话 2021-02-14 07:18

I\'ve a route as follows:

val route = {
    logRequestResult(\"user-service\") {
      pathPrefix(\"user\") {
        get {
          respondWithHeader(RawHeader         


        
1条回答
  •  一整个雨季
    2021-02-14 08:00

    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))
        }
      }
    }
    

    0 讨论(0)
提交回复
热议问题