Lookup custom header in response headers

65 views
Skip to first unread message

Kyrylo Stokoz

unread,
Apr 21, 2017, 3:23:49 AM4/21/17
to Akka User List
Hi All,

I`m trying to extract custom header from response object like this:

response.header[`X-Transaction-Id`]

but it always give me None, changing it to lookup by name return expected value.

response.headers.find(_.lowercaseName() == `X-Transaction-Id`.name.toLowerCase)

It looks like it is related to modeling custom headers as RawHeader.
According to http://doc.akka.io/docs/akka-http/10.0.5/scala/http/common/http-model.html:
"Thanks to extending ModeledCustomHeader instead of the plain CustomHeader such header can be matched"

Should it apply to header extraction in the way above as well? Is it a bug? or as designed?

Regards,
Kyrylo 

Konrad Malawski

unread,
Apr 24, 2017, 5:46:13 AM4/24/17
to Akka User List
Hi Kyrylo,
Would you mind sharing your code for the transaction id header?

That way of extracting a header does work, and I've added a test as we were missing that specific style actually:

I suspect you may have done something wrong in your header definition - are you sure you defined everything that's needed?



--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+unsubscribe@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Cheers,
Konrad 'ktoso' Malawski

Marco Yuen

unread,
May 7, 2017, 7:56:24 AM5/7/17
to Akka User List
Hi Konrad,

I'm don't mean to hijack the thread, but I'm having the same question.
Here is the custom header:
final case class RateLimitLimit(limit: String) extends ModeledCustomHeader[RateLimitLimit] {
override def companion: ModeledCustomHeaderCompanion[RateLimitLimit] = RateLimitLimit

override def renderInRequests(): Boolean = true

override def renderInResponses(): Boolean = true

override def value(): String = limit
}

object RateLimitLimit extends ModeledCustomHeaderCompanion[RateLimitLimit] {
override def name: String = "X-RateLimit-Limit"

override def parse(value: String): Try[RateLimitLimit] = Success(RateLimitLimit(value))
}

When I tried to extract the header from HttpResponse using resp.header[RateLimitLimit], I always get None. However, if I use resp.header[RawHeader], I will get my custom header back, but only if there only one custom header in the response. If the response contains more than one custom headers, resp.header[RawHeader] will return first custom header.

I'm using akka-http 10.0.6 and akka 2.5.1.

Best,
Marco
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.

To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Greg Methvin

unread,
May 7, 2017, 11:30:50 PM5/7/17
to akka...@googlegroups.com
It works in Konrad's unit test because that actually inserts an instance of the custom header class into the headers, as opposed to having Akka HTTP parse the response headers from an actual HTTP response. I don't think the header parsing code is able to parse to the custom header types.

Greg

To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+unsubscribe@googlegroups.com.

To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Greg Methvin
Tech Lead - Play Framework

Konrad Malawski

unread,
May 8, 2017, 1:27:36 AM5/8/17
to akka...@googlegroups.com, Marco Yuen
To clarify: You'll notice that it does actually work in the routing DSL, even if you supply a RawHeader it will attempt to extract the typed modeled header that you provide it with. 

This actually does work in the routing DSL:

      val routes = headerValueByType[ApiTokenHeader]() { token ⇒
        complete(s"extracted> $token")
      }

      Get().withHeaders(RawHeader("apiKey", "TheKey")) ~> routes ~> check {
        status should ===(StatusCodes.OK)
        responseAs[String] should ===("extracted> apiKey: TheKey") // so it matched the custom one
      }


Seems we omitted to add the special logic that can do this to the HttpRequest / HttpResponse itself, since they are "low level", 
and usually did not do these things... I'll take a look if we could add that infrastructure there without breaking binary compatibility.

-- 
Konrad `ktoso` Malawski
Akka @ Lightbend

Greg Methvin

unread,
May 8, 2017, 3:21:44 AM5/8/17
to akka...@googlegroups.com, Konrad Malawski, Marco Yuen
Okay, that makes sense. Thanks Konrad.

To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+unsubscribe@googlegroups.com.

To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+unsubscribe@googlegroups.com.

To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--

Konrad Malawski

unread,
May 8, 2017, 5:42:13 AM5/8/17
to Greg Methvin, Akka User List, Marco Yuen
I made it work in Scala: https://github.com/akka/akka-http/pull/1103
It's a bit sneaky internally, but I'd be ok with such impl.
Sadly it does not translate directly to JavaDSL and that we'll have to deal with in a different style.
Cheers,
Konrad 'ktoso' Malawski
Reply all
Reply to author
Forward
0 new messages