5

I'm using OpenAPI Generator to generate a Java 11 HTTP Client for a swagger-compliant service.

The thing is: this service requires basic authentication but it doesn't challenge the client with a WWW-Authenticate header, so the Java 11 HTTP Client won't send the basic authentication configured with the builder. The workaround for this is to add the Authorization header directly in the request, as suggested in this thread.

Now: as of version 4.3.1, OpenAPI Generator won't expose me an interface to customize the HTTP requests, so I'm wondering if it would be best to override the responsible template (api.mustache:126) or if there is a cleaner alternative for this task.

cansi
  • 83
  • 1
  • 6

1 Answers1

2

I come with same issue as yours and solution for me was using generated ApiClient class and its RequestInterceptor field. It allows to customize request headers prior sending it to server:

ApiClient client = new ApiClient();
client.setRequestInterceptor(new Consumer<HttpRequest.Builder>() {
  @Override
  public void accept(HttpRequest.Builder builder) {
    builder.header("Authorization", "Basic " + Base64.getEncoder().encodeToString("foo:bar"));
  }
});

There is also a backing version of above called response interceptor if you need to capture reply headers.

splatch
  • 1,547
  • 2
  • 10
  • 15