为什么 HTTPBuilder 会抛出 "HttpResponseException: Bad Request"?

Why does HTTPBuilder throw "HttpResponseException: Bad Request"?

Http-builder:0.7.1 | 语言:Groovy | 框架:Spock

测试代码

import groovyx.net.http.HTTPBuilder
import spock.lang.Specification

/**
 * Created by Long Nguyen on 4/11/2017.
 *
 * Chatwork api documentation: http://developer.chatwork.com/ja/index.html
 */
class ChatworkApiSpec extends Specification {
    // https://api.chatwork.com/v2/contacts
    def apiRoot = "http://api.chatwork.com/v2"
    def contactsPath = "/contacts"
    def apiToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx"
    def http = new HTTPBuilder(apiRoot)

    /**
     * Endpoint: /contacts
     * You can access the list of users who are in contact with you.
     */
    def "Get your contact list"() {
        when:
        def response = http.get(path: contactsPath, headers: ["X-ChatWorkToken": apiToken])
        def responseData = response.responseData
        then:
        println responseData
    }
}

它总是抛出以下异常。我不知道设置 headers.

有什么问题

(当然,当我使用 Postman 时,这个 api 对我有用)

留言:

groovyx.net.http.HttpResponseException: Bad Request

    at groovyx.net.http.HTTPBuilder.defaultFailureHandler(HTTPBuilder.java:652)
    at groovy.lang.Closure.call(Closure.java:414)
    at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:508)
    at groovyx.net.http.HTTPBuilder.get(HTTPBuilder.java:292)
    at groovyx.net.http.HTTPBuilder.get(HTTPBuilder.java:262)
    at testcase.ChatworkApiSpec.Get your contact list(ChatworkApiSpec.groovy:24)

对不起,我的问题不好。我发布我的根 link 也包含路径。

替换:

def apiRoot = "http://api.chatwork.com/v2"
def contactsPath = "/contacts"

来自

def apiRoot = "http://api.chatwork.com"
def contactsPath = "/v2/contacts"

可以解决这个问题。