首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用fuel的具有多个标头信息的post请求

使用fuel的具有多个标头信息的post请求
EN

Stack Overflow用户
提问于 2019-03-05 20:20:54
回答 2查看 1.1K关注 0票数 0

我尝试使用燃料框架(https://github.com/kittinunf/Fuel)发送一个用kotlin和b编写的post请求。但是,我需要发送一个带有post请求和基本身份验证凭据的json主体。

这是我当前的尝试,它总是导致HTTP异常400。所以我有种感觉,我发送身体的方式有问题。我就是弄不明白它是什么:

代码语言:javascript
复制
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请求如下所示:

代码语言:javascript
复制
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"
    ]
}'
EN

回答 2

Stack Overflow用户

发布于 2019-03-05 21:58:14

尝试手动执行该请求。使用fiddler/postman/您首选的HTTP客户端。也许你在正文或标题中遗漏了什么?

另外,我还推荐使用原始字符串。它的可读性更好。

代码语言:javascript
复制
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") })
票数 0
EN

Stack Overflow用户

发布于 2019-03-06 19:41:33

我尝试了一下,发现必须使用身份验证().basic()才能正确访问,而不是使用header()。这就是结果:

代码语言:javascript
复制
    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") })
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55002709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档