在Spray POST路由内提取原始JSON作为字符串 [英] Extracting Raw JSON as String inside a Spray POST route
问题描述
我有一个POST Spray路由,并且请求包含JSON正文(内容类型为"application/json").我想要一种从我的路线内的此请求中提取原始JSON的方法.
I've a POST Spray route and the request contains a JSON body (content-type "application/json"). I want a way to extract the raw JSON from this request inside my route.
对于 http://host:port/somepath/value1 我想提取该帖子正文为TextMsgResponse
.但是对于 http://host:port/somepath/value2 我想提取帖子正文作为原始的Json(例如,{ "name":"Jack", "age":30 }
For http://host:port/somepath/value1 I want to extract the post body as TextMsgResponse
. But for http://host:port/somepath/value2 I want to extract the post body just as a raw Json (e.g., { "name":"Jack", "age":30 }
val myRoute = path("somepath" / Segment) { pathSegment =>
post { //use only POST requests
pathSegment match {
case "value1" =>
entity(as[TextMsgResponse]) { textMsg =>
complete {
//do something with the request
StatusCodes.OK
}
}
case "value2" => {
//here is I want to extract the RAW JSON from the request
}
}
}
推荐答案
您可以使用
这篇关于在Spray POST路由内提取原始JSON作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!