加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 如何在Akka-http 2.4.1中包含application / x-www-form

发布时间:2020-12-16 18:46:01 所属栏目:安全 来源:网络整理
导读:我正在使用Akka Http 2.4.1向twitter api发布https请求. 根据他们的documentation,我需要两个httpheaders.即,授权和ContentType. 引用他们的文档: 请求必须包含一个Content-Type标头,其值为application / x-www-form-urlencoded; charset = UTF-8. 这是我的
我正在使用Akka Http 2.4.1向twitter api发布https请求.

根据他们的documentation,我需要两个httpheaders.即,授权和ContentType.

引用他们的文档:

请求必须包含一个Content-Type标头,其值为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`))

如何使用值application / x-www-form-urlencoded包含Content-Type标头;使用Akka-http 2.4.1包含charset = UTF-8?

解决方法

我想如果你将HttpRequest上的实体值更改为FormData,如下所示:

HttpRequest(
    uri = Uri("https://api.twitter.com/oauth2/token"),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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读