我尝试使用燃料框架(https://github.com/kittinunf/Fuel)发送一个用kotlin和b编写的post请求。但是,我需要发送一个带有post请求和基本身份验证凭据的json主体。
这是我当前的尝试,它总是导致HTTP异常400。所以我有种感觉,我发送身体的方式有问题。我就是弄不明白它是什么:
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") })postman生成的正常运行的cURL请求如下所示:
curl -X POST \
https://atc.mywebpage.net/jira/rest/api/2/search \
-H 'Content-Type: application/json' \
-H 'Postman-Token: xxxxxxxxxxxxxxxxxxxxxxx' \
-H 'cache-control: no-cache' \
-d '{
"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"
]
}'发布于 2019-03-05 21:58:14
尝试手动执行该请求。使用fiddler/postman/您首选的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") })发布于 2019-03-06 19:41:33
我尝试了一下,发现必须使用身份验证().basic()才能正确访问,而不是使用header()。这就是结果:
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") })
}https://stackoverflow.com/questions/55002709
复制相似问题