Akka-http:如何从请求中获取自定义标头? [英] Akka-http: How to get custom header from a request?

查看:96
本文介绍了Akka-http:如何从请求中获取自定义标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在请求中向我的Akka-http api发送以下标头: Content-type: application / json 接受: application / json AppId: some_id

I send following headers in a request to my akka-http api: "Content-type": "application/json", "Accept": "application/json", "AppId": "some_id".

如何在我的Akka-http路由中获取 AppId自定义标头?

How do I get "AppId" custom header in my akka-http route?

(get & parameters("id")) { (id) =>
      complete {
        val appId = ?? // I want to get custom header here.
      }
    } 

谢谢。

推荐答案

您需要使用 HeaderDirectives HeaderDirectives文档)以提取标头。例如,如果它是自定义的,则可以使用 headerValueByName 产生标头的值,如果标头不存在,则拒绝路由(如果标头是可选的,则可以使用 optionalHeaderValueByName ):

You need to use one of the HeaderDirectives (HeaderDirectives docs) to extract the header. For example, if it's a custom one you can use headerValueByName which yields the value of the header, and rejects the route if the header was not present (if the header is optional you can use optionalHeaderValueByName):

headerValueByName("AppId") { appId =>
  complete(s"The AppId was: $appId")
}

祝你快乐!

这篇关于Akka-http:如何从请求中获取自定义标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆