gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:10:58 27 4
gpt4 key购买 nike

我正在使用 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) HttpRequestFormData像这样:

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com