无法在 Spray 中为内容类型请求设置 header
Not able to set header for content type request in spray
我验证用户登录的一段代码是:
val loginRoute = path("login") {
post {
parameter('next ?) {
(next) =>
entity(as[FormData]) {
params =>
implicit ctx => {
var headers = List[HttpHeader]()
val user = params.fields.find(_._1 == "username").get._2
val pass = params.fields.find(_._1 == "password").get._2
val remember = params.fields.find(_._1 == "remember") match {
case Some(rem) => rem._2
case None => "off"
}
LdapAuthenticationProvider.authenticate(user, pass) match {
case false =>
sendResponse(StatusCodes.Forbidden.intValue, "Authentication Failed")
redirect("login", StatusCodes.Found)
case true =>
if ("on".equalsIgnoreCase(remember)) {
val hash = calculateHash(Map(Const.USERNAME -> user))
headers = List(HttpHeaders.`Set-Cookie`(HttpCookie(Const.COOKIE_REMEMBER_ME_KEY, user)),
HttpHeaders.`Set-Cookie`(HttpCookie(Const.COOKIE_AUTH_HASH, hash)))
}
val url = next match {
case Some(path) => path
case None => "/"
}
complete {
HttpResponse(
status = StatusCodes.Found,
headers = Location(url) :: headers,
entity = StatusCodes.Found.htmlTemplate match {
case "" ⇒ HttpEntity.Empty
case template ⇒ HttpEntity(`text/html`, template format url)
}
)
}
}
}
}
}
}
}
但是我无法设置请求内容类型。喷雾不应该自动为我做吗?我的请求将是
Content-Type: application/x-www-form-urlencoded
我在 POST 请求 header 中设置的。
但是我仍然得到:
There was a problem with the requests Content-Type:
Expected 'application/json'
请帮帮我!提前致谢!
Jrudolf 提出的问题是我的 JSON 支持特征试图反序列化数据。 headers 被忽略了。事实上,它甚至没有到达那里。解决方案就像扩展现有的编组器一样简单。
class LoginServlet(context: akka.actor.ActorRefFactory) extends BaseServlet with FormDataUnmarshallers {
}
我验证用户登录的一段代码是:
val loginRoute = path("login") {
post {
parameter('next ?) {
(next) =>
entity(as[FormData]) {
params =>
implicit ctx => {
var headers = List[HttpHeader]()
val user = params.fields.find(_._1 == "username").get._2
val pass = params.fields.find(_._1 == "password").get._2
val remember = params.fields.find(_._1 == "remember") match {
case Some(rem) => rem._2
case None => "off"
}
LdapAuthenticationProvider.authenticate(user, pass) match {
case false =>
sendResponse(StatusCodes.Forbidden.intValue, "Authentication Failed")
redirect("login", StatusCodes.Found)
case true =>
if ("on".equalsIgnoreCase(remember)) {
val hash = calculateHash(Map(Const.USERNAME -> user))
headers = List(HttpHeaders.`Set-Cookie`(HttpCookie(Const.COOKIE_REMEMBER_ME_KEY, user)),
HttpHeaders.`Set-Cookie`(HttpCookie(Const.COOKIE_AUTH_HASH, hash)))
}
val url = next match {
case Some(path) => path
case None => "/"
}
complete {
HttpResponse(
status = StatusCodes.Found,
headers = Location(url) :: headers,
entity = StatusCodes.Found.htmlTemplate match {
case "" ⇒ HttpEntity.Empty
case template ⇒ HttpEntity(`text/html`, template format url)
}
)
}
}
}
}
}
}
}
但是我无法设置请求内容类型。喷雾不应该自动为我做吗?我的请求将是
Content-Type: application/x-www-form-urlencoded
我在 POST 请求 header 中设置的。 但是我仍然得到:
There was a problem with the requests Content-Type:
Expected 'application/json'
请帮帮我!提前致谢!
Jrudolf 提出的问题是我的 JSON 支持特征试图反序列化数据。 headers 被忽略了。事实上,它甚至没有到达那里。解决方案就像扩展现有的编组器一样简单。
class LoginServlet(context: akka.actor.ActorRefFactory) extends BaseServlet with FormDataUnmarshallers {
}