0

Http-builder: 0.7.1 | Language: Groovy | Framework: Spock

Testing Code

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
    }
}

It always throws below exception. I don't know what i was wrong with setting headers.

(Of course this api works for me when I use Postman)

Message:

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)
kriegaex
  • 63,017
  • 15
  • 111
  • 202
Long Nguyen
  • 9,898
  • 5
  • 53
  • 52

1 Answers1

3

Sorry my bad question. I release that my root link also included path in it.

Replace:

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

by

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

can resolve this problem.

Long Nguyen
  • 9,898
  • 5
  • 53
  • 52
  • 1
    As an author, you can always delete a question. And this is what I recommend you to do. – kriegaex Apr 11 '17 at 11:42
  • 1
    Maybe someone will have same problem like me, i think it will help alot – Long Nguyen Apr 12 '17 at 00:30
  • 1
    Well, then you should have adjusted the title to the actual problem. I have just done that for you. The problem is not about setting headers as you said. As soon as you have enough reputation to do so, please accept your own answer in order to close the question. Thank you. – kriegaex Apr 12 '17 at 08:38
  • Thanks @kriegaex: I will accept my answer after 6 hours. "You can accept your own answer in 6 hours" – Long Nguyen Apr 13 '17 at 01:13