gpt4 book ai didi

scala - 喷射 IO,为响应添加 header

转载 作者:行者123 更新时间:2023-12-02 09:36:50 26 4
gpt4 key购买 nike

我有(以前的)REST Spray.io Web 服务。现在,我需要在我的方法之一中生成 SESSIONID 以便与其他一些方法一起使用。我希望它位于响应 header 中。

基本上,我想象的逻辑如下:

 path("/...") {
get {
complete {
// some logic here
// .....
someResult match {
case Some(something) =>
val sessionID = generateSessionID
session(sessionID) = attachSomeData(something)
// here I need help how to do my imaginary respond with header
[ respond-with-header ? ]("X-My-SessionId", sessionID) {
someDataMarshalledToJSON(something)
}


case None => throw .... // wrapped using error handler
}
}
}
}

但是,它不能在完整的内部工作,我的意思是 respondWithHeader 指令。我需要建议。

最佳答案

Spray 中有一个 respondWithHeader 指令。这里是官方doc以及如何使用它的示例:

 def respondWithSessionId(sessionID: String) =
respondWithHeader(RawHeader("X-My-SessionId", sessionID))

path("/...") {
get {
// some logic here
// .....
sessionIDProvider { sessionID =>
respondWithMediaType(`application/json`) { // optionally add this if you want
respondWithSessionId(sessionID) {
complete(someDataMarshalledToJSON(something))
}
}
}
}
}

关于scala - 喷射 IO,为响应添加 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24990880/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com