作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Akka Http 2.4.1 向 twitter api 发布 https 请求。
据他们documentation ,我需要两个httpheader。即,授权和内容类型。
引用他们的文档:
该请求必须包含一个 Content-Type header ,其值为 application/x-www-form-urlencoded;charset=UTF-8。
这是我的代码:
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`))
Content-Type
带有值
application/x-www-form-urlencoded;charset=UTF-8
的 header 使用 Akka-http 2.4.1?
最佳答案
我想如果你改变entity
您的值(value) HttpRequest
至 FormData
像这样:
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`)
)
Content-Type
为您自动设置为
application/x-www-form-urlencoded;charset=UTF-8
关于scala - 如何在 Akka-http 2.4.1 中包含 application/x-www-form-urlencoded HttpHeader?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35468676/
我是一名优秀的程序员,十分优秀!