Interface HttpClient

All Superinterfaces:
AutoCloseable, Closeable, io.micronaut.context.LifeCycle<HttpClient>
All Known Subinterfaces:
StreamingHttpClient

public interface HttpClient extends Closeable, io.micronaut.context.LifeCycle<HttpClient>
A non-blocking HTTP client interface designed around the Micronaut API and Reactive Streams.
Since:
1.0
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final io.micronaut.core.type.Argument<io.micronaut.http.hateoas.JsonError>
    The default error type.
  • Method Summary

    Modifier and Type
    Method
    Description
    static HttpClient
    create(@Nullable URL url)
    Create a new HttpClient.
    static HttpClient
    create(@Nullable URL url, @NonNull HttpClientConfiguration configuration)
    Create a new HttpClient with the specified configuration.
    default <I> org.reactivestreams.Publisher<io.micronaut.http.HttpResponse<io.micronaut.core.io.buffer.ByteBuffer>>
    exchange(@NonNull io.micronaut.http.HttpRequest<I> request)
    Perform an HTTP request for the given request object emitting the full HTTP response from returned Publisher.
    default <I, O> org.reactivestreams.Publisher<io.micronaut.http.HttpResponse<O>>
    exchange(@NonNull io.micronaut.http.HttpRequest<I> request, @NonNull io.micronaut.core.type.Argument<O> bodyType)
    Perform an HTTP request for the given request object emitting the full HTTP response from returned Publisher and converting the response body to the specified type.
    <I, O, E> org.reactivestreams.Publisher<io.micronaut.http.HttpResponse<O>>
    exchange(@NonNull io.micronaut.http.HttpRequest<I> request, @NonNull io.micronaut.core.type.Argument<O> bodyType, @NonNull io.micronaut.core.type.Argument<E> errorType)
    Perform an HTTP request for the given request object emitting the full HTTP response from returned Publisher and converting the response body to the specified type.
    default <I, O> org.reactivestreams.Publisher<io.micronaut.http.HttpResponse<O>>
    exchange(@NonNull io.micronaut.http.HttpRequest<I> request, @NonNull Class<O> bodyType)
    Perform an HTTP request for the given request object emitting the full HTTP response from returned Publisher and converting the response body to the specified type.
    default org.reactivestreams.Publisher<io.micronaut.http.HttpResponse<io.micronaut.core.io.buffer.ByteBuffer>>
    exchange(@NonNull String uri)
    Perform an HTTP GET request for the given request object emitting the full HTTP response from returned Publisher.
    default <O> org.reactivestreams.Publisher<io.micronaut.http.HttpResponse<O>>
    exchange(@NonNull String uri, @NonNull Class<O> bodyType)
    Perform an HTTP GET request for the given request object emitting the full HTTP response from returned Publisher.
    default HttpClient
     
    default <I> org.reactivestreams.Publisher<String>
    retrieve(@NonNull io.micronaut.http.HttpRequest<I> request)
    Perform an HTTP request for the given request object emitting the full HTTP response from returned Publisher and converting the response body to the specified type.
    default <I, O> org.reactivestreams.Publisher<O>
    retrieve(@NonNull io.micronaut.http.HttpRequest<I> request, @NonNull io.micronaut.core.type.Argument<O> bodyType)
    Perform an HTTP request for the given request object emitting the full HTTP response from returned Publisher and converting the response body to the specified type.
    default <I, O, E> org.reactivestreams.Publisher<O>
    retrieve(@NonNull io.micronaut.http.HttpRequest<I> request, @NonNull io.micronaut.core.type.Argument<O> bodyType, @NonNull io.micronaut.core.type.Argument<E> errorType)
    Perform an HTTP request for the given request object emitting the full HTTP response from returned Publisher and converting the response body to the specified type.
    default <I, O> org.reactivestreams.Publisher<O>
    retrieve(@NonNull io.micronaut.http.HttpRequest<I> request, @NonNull Class<O> bodyType)
    Perform an HTTP request for the given request object emitting the full HTTP response from returned Publisher and converting the response body to the specified type.
    default org.reactivestreams.Publisher<String>
    retrieve(@NonNull String uri)
    Perform an HTTP GET request for the given request object emitting the full HTTP response from returned Publisher and converting the response body to the specified type.
     

    Methods inherited from interface io.micronaut.context.LifeCycle

    close, isRunning, start, stop
  • Field Details

    • DEFAULT_ERROR_TYPE

      static final io.micronaut.core.type.Argument<io.micronaut.http.hateoas.JsonError> DEFAULT_ERROR_TYPE
      The default error type.
  • Method Details

    • toBlocking

      BlockingHttpClient toBlocking()
      Returns:
      A blocking HTTP client suitable for testing and non-production scenarios.
    • exchange

      <I, O, E> org.reactivestreams.Publisher<io.micronaut.http.HttpResponse<O>> exchange(@NonNull @NonNull io.micronaut.http.HttpRequest<I> request, @NonNull @NonNull io.micronaut.core.type.Argument<O> bodyType, @NonNull @NonNull io.micronaut.core.type.Argument<E> errorType)

      Perform an HTTP request for the given request object emitting the full HTTP response from returned Publisher and converting the response body to the specified type.

      This method will send a Content-Length header and except a content length header the response and is designed for simple non-streaming exchanges of data

      By default the exchange Content-Type is application/json, unless otherwise specified in the passed HttpRequest

      Type Parameters:
      I - The request body type
      O - The response body type
      E - The error type
      Parameters:
      request - The HttpRequest to execute
      bodyType - The body type
      errorType - The error type
      Returns:
      A Publisher that emits the full HttpResponse object
    • exchange

      default <I, O> org.reactivestreams.Publisher<io.micronaut.http.HttpResponse<O>> exchange(@NonNull @NonNull io.micronaut.http.HttpRequest<I> request, @NonNull @NonNull io.micronaut.core.type.Argument<O> bodyType)

      Perform an HTTP request for the given request object emitting the full HTTP response from returned Publisher and converting the response body to the specified type.

      This method will send a Content-Length header and except a content length header the response and is designed for simple non-streaming exchanges of data

      By default the exchange Content-Type is application/json, unless otherwise specified in the passed HttpRequest

      Type Parameters:
      I - The request body type
      O - The response body type
      Parameters:
      request - The HttpRequest to execute
      bodyType - The body type
      Returns:
      A Publisher that emits the full HttpResponse object
    • exchange

      default <I> org.reactivestreams.Publisher<io.micronaut.http.HttpResponse<io.micronaut.core.io.buffer.ByteBuffer>> exchange(@NonNull @NonNull io.micronaut.http.HttpRequest<I> request)
      Perform an HTTP request for the given request object emitting the full HTTP response from returned Publisher.
      Type Parameters:
      I - The request body type
      Parameters:
      request - The HttpRequest to execute
      Returns:
      A Publisher that emits the full HttpResponse object
    • exchange

      default org.reactivestreams.Publisher<io.micronaut.http.HttpResponse<io.micronaut.core.io.buffer.ByteBuffer>> exchange(@NonNull @NonNull String uri)
      Perform an HTTP GET request for the given request object emitting the full HTTP response from returned Publisher.
      Parameters:
      uri - The Uri
      Returns:
      A Publisher that emits the full HttpResponse object
    • exchange

      default <O> org.reactivestreams.Publisher<io.micronaut.http.HttpResponse<O>> exchange(@NonNull @NonNull String uri, @NonNull @NonNull Class<O> bodyType)
      Perform an HTTP GET request for the given request object emitting the full HTTP response from returned Publisher.
      Type Parameters:
      O - The response body type
      Parameters:
      uri - The request URI
      bodyType - The body type
      Returns:
      A Publisher that emits the full HttpResponse object
    • exchange

      default <I, O> org.reactivestreams.Publisher<io.micronaut.http.HttpResponse<O>> exchange(@NonNull @NonNull io.micronaut.http.HttpRequest<I> request, @NonNull @NonNull Class<O> bodyType)
      Perform an HTTP request for the given request object emitting the full HTTP response from returned Publisher and converting the response body to the specified type.
      Type Parameters:
      I - The request body type
      O - The response body type
      Parameters:
      request - The HttpRequest to execute
      bodyType - The body type
      Returns:
      A Publisher that emits the full HttpResponse object
    • retrieve

      default <I, O, E> org.reactivestreams.Publisher<O> retrieve(@NonNull @NonNull io.micronaut.http.HttpRequest<I> request, @NonNull @NonNull io.micronaut.core.type.Argument<O> bodyType, @NonNull @NonNull io.micronaut.core.type.Argument<E> errorType)
      Perform an HTTP request for the given request object emitting the full HTTP response from returned Publisher and converting the response body to the specified type.
      Type Parameters:
      I - The request body type
      O - The response body type
      E - The error type
      Parameters:
      request - The HttpRequest to execute
      bodyType - The body type
      errorType - The error type
      Returns:
      A Publisher that emits a result of the given type
    • retrieve

      default <I, O> org.reactivestreams.Publisher<O> retrieve(@NonNull @NonNull io.micronaut.http.HttpRequest<I> request, @NonNull @NonNull io.micronaut.core.type.Argument<O> bodyType)
      Perform an HTTP request for the given request object emitting the full HTTP response from returned Publisher and converting the response body to the specified type.
      Type Parameters:
      I - The request body type
      O - The response body type
      Parameters:
      request - The HttpRequest to execute
      bodyType - The body type
      Returns:
      A Publisher that emits a result of the given type
    • retrieve

      default <I, O> org.reactivestreams.Publisher<O> retrieve(@NonNull @NonNull io.micronaut.http.HttpRequest<I> request, @NonNull @NonNull Class<O> bodyType)
      Perform an HTTP request for the given request object emitting the full HTTP response from returned Publisher and converting the response body to the specified type.
      Type Parameters:
      I - The request body type
      O - The response body type
      Parameters:
      request - The HttpRequest to execute
      bodyType - The body type
      Returns:
      A Publisher that emits a result of the given type
    • retrieve

      default <I> org.reactivestreams.Publisher<String> retrieve(@NonNull @NonNull io.micronaut.http.HttpRequest<I> request)
      Perform an HTTP request for the given request object emitting the full HTTP response from returned Publisher and converting the response body to the specified type.
      Type Parameters:
      I - The request body type
      Parameters:
      request - The HttpRequest to execute
      Returns:
      A Publisher that emits String result
    • retrieve

      default org.reactivestreams.Publisher<String> retrieve(@NonNull @NonNull String uri)
      Perform an HTTP GET request for the given request object emitting the full HTTP response from returned Publisher and converting the response body to the specified type.
      Parameters:
      uri - The URI
      Returns:
      A Publisher that emits String result
    • refresh

      default HttpClient refresh()
      Specified by:
      refresh in interface io.micronaut.context.LifeCycle<HttpClient>
    • create

      static HttpClient create(@Nullable @Nullable URL url)
      Create a new HttpClient. Note that this method should only be used outside of the context of a Micronaut application. The returned HttpClient is not subject to dependency injection. The creator is responsible for closing the client to avoid leaking connections. Within a Micronaut application use Inject to inject a client instead.
      Parameters:
      url - The base URL
      Returns:
      The client
    • create

      static HttpClient create(@Nullable @Nullable URL url, @NonNull @NonNull HttpClientConfiguration configuration)
      Create a new HttpClient with the specified configuration. Note that this method should only be used outside of the context of an application. Within Micronaut use Inject to inject a client instead
      Parameters:
      url - The base URL
      configuration - the client configuration
      Returns:
      The client
      Since:
      2.2.0