Skip to content

Can't set http headers for authentication #74

@zcmgyu

Description

@zcmgyu

Http-builder: 0.7.1
Language: Groovy
Framework: 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
    }
}

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)

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions