jgritman / httpbuilder

315 stars 154 forks source link

Basic Authentication does not work with POST in AsyncHttpBuilder #47

Closed renatoathaydes closed 9 years ago

renatoathaydes commented 9 years ago

Here's my code:

final client = new AsyncHTTPBuilder(uri: 'http://localhost:8443', contentType: JSON).with {
    auth.basic('client-one', '0ne!Secret')
    return it
}

final future = client.post(
        path: '/dev/oauth/token',
        body: ['scope'     : 'read write',
               'grant_type': 'client_credentials',
               'client_id' : 'client-one'],
) { resp, json ->
    assert resp.statusLine.statusCode == 200
    return json
} as Future

def json = future.get(*requestTimeout)

The server just does not receive the authorization header. Am I doing something wrong or is this a problem with the builder?

renatoathaydes commented 9 years ago

I think this is because of the HTTP Client, which does not send the credentials in the first request, only providing it in case the server returns a 401 first.

It would be really nice to be able to force the HTTP client to send the credentials in the first try... I've read the docs of HTTP Client and I know they do not want to encourage this, but for testing, at least, this is essential.

renatoathaydes commented 9 years ago

Confirmed the above. The server did not send the WWW-Authenticate header, that's why the http client did not bother trying to send credentials. But I still would really appreciate if it would be possible to send the credentials in the first try... I might make a PR adding that if you think it's a good idea.

jjathman commented 9 years ago

Instead of using the helper methods for sending the basic auth credentials, you'll just need to build the auth header and value yourself. It's pretty simple code to write. There isn't any way I know of to force the HTTP client to pass them on the first call.

renatoathaydes commented 9 years ago

I agree. Thanks.