How to set BearerToken in Akka Http Client single request

Hi all,

I’m receiving a warning from akka http when trying to make a POST request with a bearer token.

Here is the snippet where I am making the request.

val bearerToken: String = "..." 

val resp = Http(context.system)
      .singleRequest(
        HttpRequest(
          HttpMethods.POST,
          endpoint,
          entity = HttpEntity(MediaTypes.`application/json`, payloadJson)
        ).withHeaders(Authorization(OAuth2BearerToken(bearerToken))) 
      )

I then see this in the log:
WARN akka.actor.ActorSystemImpl - Illegal header: Illegal ‘authorization’ header: Invalid input ‘2’, expected ‘=’, OWS or ‘EOI’ (line 1, column 1152): Bearer token-string-here

And I get back a 400 from the external endpoint, so it seems that I’m doing something wrong in the way I am building the request. Any tips?

should the token be encoded in some way?

From a quick glance at RFC 6750 it seems the token is quite limited in what characters it may contain, perhaps that is the problem?

See: RFC 6750 - The OAuth 2.0 Authorization Framework: Bearer Token Usage

1 Like

I think the issue was that I had an extra / in the url. It seems a little strange that I was getting this message because of the extra /, but after using the correct URL, it works.