2016-02-17 9 views
5

Sto usando Akka Http 2.4.1 per pubblicare una richiesta https su twitter api.Come includere application/x-www-form-urlencoded HttpHeader in Akka-http 2.4.1?

In base al loro documentation, ho bisogno di due httpheaders. Vale a dire, Autorizzazione e ContentType.

per citare i loro documenti:

La richiesta deve includere un header Content-Type con il valore di application/x-www-form-urlencoded; charset = UTF-8.

Ecco il mio codice:

val authorization = Authorization(BasicHttpCredentials(key, secret)) 

/* 
    This header is removed from the request with the following explanation! 
    "Explicitly set HTTP header 'Content-Type: application/x-www-form-urlencoded;' is ignored, illegal RawHeader" 
*/ 
val contentType = RawHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8") 

Http().singleRequest(
    HttpRequest(
     uri = Uri("https://api.twitter.com/oauth2/token"), 
     method = HttpMethods.POST, 
     headers = List(authorization, contentType), 
     entity = HttpEntity(`text/plain(UTF-8)`, "grant_type=client_credentials"), 
     protocol = HttpProtocols.`HTTP/1.1`)) 

Come posso includere l'intestazione Content-Type con il valore application/x-www-form-urlencoded;charset=UTF-8 utilizzando Akka-http 2.4.1?

+1

Content-Type sarà impostata automaticamente in base al tipo di entità inclusa nella richiesta. Nel tuo caso stai impostando il tipo di contenuto delle tue entità http su 'text/plain (UTF-8)'. – cmbaxter

+0

Grazie per la spiegazione aggiuntiva. Utile! – rodoherty1

risposta

14

penso che se si modifica il valore entity sul HttpRequest-FormData in questo modo:

HttpRequest(
    uri = Uri("https://api.twitter.com/oauth2/token"), 
    method = HttpMethods.POST, 
    headers = List(authorization), 
    entity = FormData("grant_type" -> "client_credentials").toEntity, 
    protocol = HttpProtocols.`HTTP/1.1`) 
) 

Poi si dovrebbe ottenere il Content-Type da impostare automaticamente per voi a application/x-www-form-urlencoded;charset=UTF-8

Problemi correlati