我尝试使用Fuel框架(https://github.com/kittinunf/Fuel)发送用kotlin和b编写的发帖请求。但是,我需要发送带有发布请求以及基本身份验证凭据的json正文。
这是我目前的尝试,始终尝试使用HTTP Exception400。因此,我感到发送正文的方式有问题。我只是不知道这是什么:
if constexpr
邮递员生成的有效cURL请求如下所示:
val myJsonBody = " {\n" +
" \"jql\": \"component = LOLO AND fixVersion = '18/3 Patch-2'\",\n" +
" \"startAt\": 0,\n" +
" \"maxResults\": 300,\n" +
" \"fields\": [\n" +
" \"issuetype\",\n" +
" \"created\",\n" +
" \"status\",\n" +
" \"summary\",\n" +
" \"customfield_10002\",\n" +
" \"customfield_10003\",\n" +
" \"customfield_11201\",\n" +
" \"customfield_10006\"\n" +
" ]\n" +
" }"
val confluenceUrl = "https://atc.mywebpage.net/jira/rest/api/2/search"
val (ignoredRequest, ignoredResponse, result) =
Fuel.post(confluenceUrl)
.header("Content-Type", "application/json")
.header(user,password)
.jsonBody(myJsonBody)
.responseString ()
result.fold({ print("success: $result") }, { print("failure: $result") })
答案 0 :(得分:0)
尝试手动执行该请求。使用小提琴手/邮递员/您的首选HTTP客户端。也许您在正文或标题中缺少某些内容?
我还建议使用原始字符串文字。它更具可读性。
val myJsonBody = """
{
"jql": "component = LOLO AND fixVersion = '18/3 Patch-2'",
"startAt": 0,
"maxResults": 300,
"fields": [
"issuetype",
"created",
"status",
"summary",
"customfield_10002",
"customfield_10003",
"customfield_11201",
"customfield_10006"
]
}
""".trimIndent()
val confluenceUrl = "https://atc.mywebpage.net/jira/rest/api/2/search"
val (ignoredRequest, ignoredResponse, result) =
Fuel.post(confluenceUrl)
.header("Content-Type", "application/json")
.header(user,password)
.jsonBody(myJsonBody)
.responseString ()
result.fold({ print("success: $result") }, { print("failure: $result") })
答案 1 :(得分:0)
I played around a little and I found out that I had to use authentication().basic() to access properly and not header(). This is the result:
val confluenceUrl = "https://atc.mywebpage.net/jira/rest/api/2/search"
val user = "myUser"
val password = "myPassword"
val myJsonBody = """
{
"jql": "component = LOLO AND fixVersion = '18/3 Patch-2'",
"startAt": 0,
"maxResults": 300,
"fields": [
"issuetype",
"created",
"status",
"summary",
"customfield_10002",
"customfield_10003",
"customfield_11201",
"customfield_10006"
]
}
""".trimIndent()
val (ignoredRequest, ignoredResponse, result) =
Fuel.post(confluenceUrl)
.header("Content-Type", "application/json")
.authentication().basic(user, password)
.jsonBody(myJsonBody)
.responseString ()
result.fold({ print("success: $result") }, { print("failure: $result") })
}