向KTOR客户端发送URLENCOD编码的邮政请求表格?
我想利用 ktor 客户端库与Web API进行交互,特别是通过Oauth进行身份验证,并且我是我的''' M试图正确地发送UrlenCod编码的POST请求时遇到麻烦。由于某种原因,实际上没有发送实际的身体。
我已经尝试在此上查看类似的问题,例如这个问题< /a>,但我相信KTOR的API一定已经改变。我还看了对这种确切类型的请求,这里) 。
我正在使用以下内容发送我的请求:
val response = client.submitForm() {
url("http://localhost:5555/oauth/token/")
parameter("code", authData.authCode)
parameter("grant_type", "authorization_code")
parameter("client_id", clientId)
parameter("client_secret", clientSecret)
header("X-API-Key", getApiKey())
method = HttpMethod.Post
}
哪些导致以下请求是从Quick Express.js应用程序记录的,我构建了以模拟请求:
----- Body -----
{}
----- Headers -----
{
host: 'localhost:5555',
'content-length': '0',
'x-api-key': 'api_key_here',
'accept-charset': 'UTF-8',
accept: '*/*',
'user-agent': 'Ktor client',
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
我的标头正好发送,但是身体是空的。
我还尝试了其他一些变体,例如:
val response = client.post("http://localhost:5555/oauth/token/") {
formData {
parameter("test", "test")
}
}
导致:
----- Body -----
{}
----- Headers -----
{
host: 'localhost:5555',
'content-length': '0',
'accept-charset': 'UTF-8',
accept: '*/*',
'user-agent': 'Ktor client'
}
哪个
val response = client.post("http://localhost:5555/oauth/token/") {
FormDataContent(Parameters.build {
append("code", authData.authCode!!)
append("grant_type", "authorization_code")
append("client_id", clientId)
append("client_secret", clientSecret)
})
header("X-API-Key", getApiKey())
}
与第一组日志相似的形状:
----- Body -----
{}
----- Headers -----
{
host: 'localhost:5555',
'content-length': '0',
'x-api-key': 'api_key_here',
'accept-charset': 'UTF-8',
accept: '*/*',
'user-agent': 'Ktor client'
}
对于其他上下文,我使用以下库 /版本:
io 。
-
-
- 对于Java 18的目标,针对Java 8(这对更改构建目标也没有任何影响),
我敢肯定这很简单,但是到目前为止,我尝试过的任何排列都没有运气。
I am wanting to utilize the Ktor client library to interact with a Web API, specifically to authenticate via OAuth, and I'm running into trouble attempting to send a form urlencoded POST request properly. It would appear the actual body is not being sent for some reason.
I've tried looking at similar questions on SO, such as this question but I believe Ktor's API must have changed. I've also taken a look at JetBrains' documentation on this exact type of request here.
I am using the following to send my request:
val response = client.submitForm() {
url("http://localhost:5555/oauth/token/")
parameter("code", authData.authCode)
parameter("grant_type", "authorization_code")
parameter("client_id", clientId)
parameter("client_secret", clientSecret)
header("X-API-Key", getApiKey())
method = HttpMethod.Post
}
Which results in the following request being logged from a quick Express.js application I built to mock the request:
----- Body -----
{}
----- Headers -----
{
host: 'localhost:5555',
'content-length': '0',
'x-api-key': 'api_key_here',
'accept-charset': 'UTF-8',
accept: '*/*',
'user-agent': 'Ktor client',
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
My header is being sent just fine, however the body is empty.
I've tried a few other variations as well, such as:
val response = client.post("http://localhost:5555/oauth/token/") {
formData {
parameter("test", "test")
}
}
Which results in:
----- Body -----
{}
----- Headers -----
{
host: 'localhost:5555',
'content-length': '0',
'accept-charset': 'UTF-8',
accept: '*/*',
'user-agent': 'Ktor client'
}
as well as:
val response = client.post("http://localhost:5555/oauth/token/") {
FormDataContent(Parameters.build {
append("code", authData.authCode!!)
append("grant_type", "authorization_code")
append("client_id", clientId)
append("client_secret", clientSecret)
})
header("X-API-Key", getApiKey())
}
Which shapes up similar to the first set of logs:
----- Body -----
{}
----- Headers -----
{
host: 'localhost:5555',
'content-length': '0',
'x-api-key': 'api_key_here',
'accept-charset': 'UTF-8',
accept: '*/*',
'user-agent': 'Ktor client'
}
For additional context, I am using the following libraries / versions:
io.ktor:ktor-client-core:2.0.2
io.ktor:ktor-client-cio:2.0.2
- Kotlin: 1.7.0 (have also tried 1.6.20)
- Built with Java 18, targeting Java 8 (which also has not had any effect upon changing the build target)
I'm sure it's something simple, but I've had no luck thus far with any of my permutations that I've tried.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将
formdatacontent
的实例设置为请求主体,以发送application/x-www-form-urlencoded
请求。要创建formdatacontent
类的对象,您需要将参数的实例传递给其构造函数。
You can set an instance of
FormDataContent
to a request body to sendapplication/x-www-form-urlencoded
request. To create an object ofFormDataContent
class you need to pass an instance ofParameters
to its constructor.