Reputation: 63
I have a curl of the form curl -v -u username:password -H "Content-Type: application/json"........
Not able to crack how to get the -u
part working. Have tried multiple options like
Authenticator proxyAuthenticator = new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
String credential = Credentials.basic("username", "password");
return response.request().newBuilder()
.addHeader("Proxy-Authorization", credential)
.addHeader("Content-Type", "application/json")
.build();
}
};
and
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
RequestBody requestBody = new FormBody.Builder().add("username", "password").build();
Request request = original.newBuilder()
.addHeader("Content-Type", "application/json")
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
});
Both of which are not working. Not able to get it working even by posting it as header. Please help out!
Upvotes: 2
Views: 2223
Reputation: 63
Solved it by this.
protected Authenticator getBasicAuth(final String username, final String password) {
return new Authenticator() {
private int mCounter = 0;
@Override
public Request authenticate(Route route, Response response) throws IOException {
if (mCounter++ > 0) {
return null;
} else {
String credential = Credentials.basic(username, password);
return response.request().newBuilder().header("Authorization", credential).build();
}
}
};
}
then added this as authenticator to my client.
Upvotes: 2