Azure Core provides shared primitives, abstractions, and helpers for modern Java Azure SDK client libraries
—
Comprehensive HTTP client functionality providing low-level HTTP operations, pipeline construction with middleware support, and REST proxy capabilities for method-based service clients.
Core HTTP client for sending HTTP requests with reactive and blocking patterns.
/**
* An interface to be implemented by any azure-core plugin that wishes to provide an alternate HTTP implementation.
*/
interface HttpClient {
/**
* Send the provided request asynchronously.
* @param request The HTTP request to send
* @return A Mono that emits the HTTP response
*/
Mono<HttpResponse> send(HttpRequest request);
/**
* Send the provided request asynchronously with context.
* @param request The HTTP request to send
* @param context Additional context that is passed through the Http pipeline during the service call
* @return A Mono that emits the HTTP response
*/
default Mono<HttpResponse> send(HttpRequest request, Context context);
/**
* Sends the provided request synchronously with contextual information.
* @param request The HTTP request to send
* @param context Contextual information about the request
* @return The response
*/
default HttpResponse sendSync(HttpRequest request, Context context);
/**
* Create the default HttpClient implementation.
* @return Default HttpClient instance
*/
static HttpClient createDefault();
/**
* Create the default HttpClient implementation with options.
* @param clientOptions The HttpClient options to configure the client
* @return Default HttpClient instance
*/
static HttpClient createDefault(HttpClientOptions clientOptions);
}
/**
* Service provider interface for HttpClient implementations.
*/
interface HttpClientProvider {
/**
* Creates a new HttpClient instance with default configuration.
* @return HttpClient instance
*/
HttpClient createInstance();
/**
* Creates a new HttpClient instance with the provided configuration.
* @param clientOptions Configuration for the HttpClient
* @return HttpClient instance
*/
HttpClient createInstance(HttpClientOptions clientOptions);
}Pipeline for processing HTTP requests through a series of policies for cross-cutting concerns like authentication, retry, and logging.
/**
* The HTTP pipeline that HTTP requests and responses will flow through.
*/
class HttpPipeline {
/**
* Wraps this HttpPipeline in a RestProxy.
* @param restProxyClass Interface defining REST API methods
* @return RestProxy instance
*/
public <T> T create(Class<T> restProxyClass);
/**
* Sends the HTTP request through the pipeline.
* @param httpRequest HTTP request to send
* @return Mono emitting HTTP response
*/
public Mono<HttpResponse> send(HttpRequest httpRequest);
/**
* Sends the HTTP request through the pipeline with context.
* @param httpRequest HTTP request to send
* @param context Additional context for the request
* @return Mono emitting HTTP response
*/
public Mono<HttpResponse> send(HttpRequest httpRequest, Context context);
/**
* Gets the count of policies in this pipeline.
* @return Number of policies
*/
public int getPolicyCount();
/**
* Gets the policy at the specified index.
* @param index Policy index
* @return HttpPipelinePolicy at index
*/
public HttpPipelinePolicy getPolicy(int index);
}
/**
* Builder for creating HttpPipeline instances.
*/
class HttpPipelineBuilder {
/**
* Creates a new instance of HttpPipelineBuilder.
*/
public HttpPipelineBuilder();
/**
* Sets the HttpClient for the pipeline.
* @param httpClient HTTP client implementation
* @return HttpPipelineBuilder for method chaining
*/
public HttpPipelineBuilder httpClient(HttpClient httpClient);
/**
* Adds policies to the pipeline.
* @param policies Pipeline policies to add
* @return HttpPipelineBuilder for method chaining
*/
public HttpPipelineBuilder policies(HttpPipelinePolicy... policies);
/**
* Sets the ClientOptions that will configure the pipeline.
* @param clientOptions The ClientOptions instance
* @return HttpPipelineBuilder for method chaining
*/
public HttpPipelineBuilder clientOptions(ClientOptions clientOptions);
/**
* Sets the Tracer to trace logical and HTTP calls.
* @param tracer The Tracer instance
* @return HttpPipelineBuilder for method chaining
*/
public HttpPipelineBuilder tracer(Tracer tracer);
/**
* Builds the HttpPipeline.
* @return Configured HttpPipeline instance
*/
public HttpPipeline build();
}HTTP request representation with method, URL, headers, and body support.
/**
* The outgoing Http request.
*/
class HttpRequest {
/**
* Create a new HttpRequest instance.
* @param httpMethod HTTP method
* @param url Request URL
*/
public HttpRequest(HttpMethod httpMethod, String url);
/**
* Create a new HttpRequest instance.
* @param httpMethod HTTP method
* @param url Request URL
*/
public HttpRequest(HttpMethod httpMethod, URL url);
/**
* Gets the HTTP method for this request.
* @return HTTP method
*/
public HttpMethod getHttpMethod();
/**
* Gets the target address.
* @return Target URL
*/
public URL getUrl();
/**
* Sets the request URL.
* @param url Request URL
* @return Updated HttpRequest
*/
public HttpRequest setUrl(String url);
/**
* Sets the request URL.
* @param url Request URL
* @return Updated HttpRequest
*/
public HttpRequest setUrl(URL url);
/**
* Gets the request headers.
* @return HttpHeaders containing request headers
*/
public HttpHeaders getHeaders();
/**
* Sets a request header.
* @param name Header name
* @param value Header value
* @return Updated HttpRequest
*/
public HttpRequest setHeader(String name, String value);
/**
* Sets a request header.
* @param header HttpHeader to set
* @return Updated HttpRequest
*/
public HttpRequest setHeader(HttpHeader header);
/**
* Sets request headers, replacing any existing headers.
* @param headers Headers to set
* @return Updated HttpRequest
*/
public HttpRequest setHeaders(HttpHeaders headers);
/**
* Gets the request body.
* @return Request body as BinaryData
*/
public BinaryData getBody();
/**
* Sets the request body.
* @param body Request body
* @return Updated HttpRequest
*/
public HttpRequest setBody(String body);
/**
* Sets the request body.
* @param body Request body
* @return Updated HttpRequest
*/
public HttpRequest setBody(byte[] body);
/**
* Sets the request body.
* @param body Request body
* @return Updated HttpRequest
*/
public HttpRequest setBody(BinaryData body);
/**
* Gets the request body as Flux of ByteBuffer.
* @return Request body as Flux
*/
public Flux<ByteBuffer> getBodyAsByteBuffer();
/**
* Creates a copy of this HttpRequest.
* @return Copy of this request
*/
public HttpRequest copy();
}HTTP response representation with status code, headers, and body access methods.
/**
* The incoming Http response.
*/
abstract class HttpResponse implements Closeable {
/**
* Gets the response status code.
* @return HTTP status code
*/
public abstract int getStatusCode();
/**
* Lookup a response header with the provided name.
* @param name Header name (case-insensitive)
* @return Header value or null if not found
*/
public abstract String getHeaderValue(String name);
/**
* Gets all response headers.
* @return HttpHeaders containing all response headers
*/
public abstract HttpHeaders getHeaders();
/**
* Gets the response body as Flux of ByteBuffer.
* @return Response body as Flux
*/
public abstract Flux<ByteBuffer> getBody();
/**
* Gets the response body as BinaryData.
* @return Response body as BinaryData
*/
public abstract Mono<BinaryData> getBodyAsBinaryData();
/**
* Gets the response body as String.
* @return Response body as String
*/
public abstract Mono<String> getBodyAsString();
/**
* Gets the response body as String with specified charset.
* @param charset Character encoding
* @return Response body as String
*/
public abstract Mono<String> getBodyAsString(Charset charset);
/**
* Gets the response body as InputStream.
* @return Response body as InputStream
*/
public abstract Mono<InputStream> getBodyAsInputStream();
/**
* Gets the request that resulted in this response.
* @return HttpRequest that generated this response
*/
public abstract HttpRequest getRequest();
/**
* Closes any resources associated with this HttpResponse.
*/
@Override
public void close();
}HTTP method enumeration and utilities.
/**
* Represents the HTTP methods that can be used in a request.
*/
public enum HttpMethod {
/** The HTTP GET method. */
GET,
/** The HTTP PUT method. */
PUT,
/** The HTTP POST method. */
POST,
/** The HTTP PATCH method. */
PATCH,
/** The HTTP DELETE method. */
DELETE,
/** The HTTP HEAD method. */
HEAD,
/** The HTTP OPTIONS method. */
OPTIONS,
/** The HTTP TRACE method. */
TRACE,
/** The HTTP CONNECT method. */
CONNECT
}Collection for managing HTTP headers with case-insensitive names.
/**
* A collection of headers on an HTTP request or response.
*/
class HttpHeaders implements Iterable<HttpHeader> {
/**
* Create an empty HttpHeaders instance.
*/
public HttpHeaders();
/**
* Create HttpHeaders with initial capacity.
* @param initialCapacity Initial capacity for headers map
*/
public HttpHeaders(int initialCapacity);
/**
* Create HttpHeaders from existing map.
* @param headers Map of header name-value pairs
*/
public HttpHeaders(Map<String, String> headers);
/**
* Gets the number of headers.
* @return Number of headers
*/
public int size();
/**
* Sets a header name-value pair.
* @param name Header name
* @param value Header value
* @return Updated HttpHeaders
*/
public HttpHeaders set(String name, String value);
/**
* Sets a header name-value pair.
* @param header HttpHeader to set
* @return Updated HttpHeaders
*/
public HttpHeaders set(HttpHeader header);
/**
* Adds a header name-value pair.
* @param name Header name
* @param value Header value
* @return Updated HttpHeaders
*/
public HttpHeaders add(String name, String value);
/**
* Adds a header.
* @param header HttpHeader to add
* @return Updated HttpHeaders
*/
public HttpHeaders add(HttpHeader header);
/**
* Gets the value of the header with the specified name.
* @param name Header name (case-insensitive)
* @return Header value or null if not found
*/
public String getValue(String name);
/**
* Gets all values for the header with the specified name.
* @param name Header name (case-insensitive)
* @return List of header values
*/
public List<String> getValues(String name);
/**
* Removes the header with the specified name.
* @param name Header name to remove
* @return Header value that was removed, or null
*/
public String remove(String name);
/**
* Gets all headers as a Map.
* @return Map of header name-value pairs
*/
public Map<String, String> toMap();
/**
* Gets an iterator over the headers.
* @return Iterator over HttpHeader instances
*/
@Override
public Iterator<HttpHeader> iterator();
/**
* Gets a stream of the headers.
* @return Stream of HttpHeader instances
*/
public Stream<HttpHeader> stream();
}Individual HTTP header representation.
/**
* Represents a single header within an HTTP request or response.
*/
class HttpHeader {
/**
* Create an HTTP header.
* @param name Header name
* @param value Header value
*/
public HttpHeader(String name, String value);
/**
* Create an HTTP header.
* @param name Header name
* @param values List of header values
*/
public HttpHeader(String name, List<String> values);
/**
* Gets the header name.
* @return Header name
*/
public String getName();
/**
* Gets the header value.
* @return Header value (comma-separated if multiple values)
*/
public String getValue();
/**
* Gets all header values.
* @return List of header values
*/
public List<String> getValues();
/**
* Gets the header as a string in the format "name: value".
* @return Header string representation
*/
@Override
public String toString();
}HTTP content type representation and utilities.
/**
* Represents the value of a Content-Type header as an HTTP header value.
*/
class ContentType {
/**
* Creates a ContentType from a content type string.
* @param contentType Content type string
* @return ContentType instance
*/
public static ContentType parse(String contentType);
/**
* Gets the media type (e.g., "application/json").
* @return Media type
*/
public String getType();
/**
* Gets the subtype (e.g., "json" from "application/json").
* @return Subtype
*/
public String getSubtype();
/**
* Gets the charset parameter.
* @return Charset or null if not specified
*/
public String getCharset();
/**
* Gets a parameter value.
* @param parameterName Parameter name
* @return Parameter value or null
*/
public String getParameter(String parameterName);
/**
* Gets all parameters.
* @return Map of parameter name-value pairs
*/
public Map<String, String> getParameters();
/**
* Converts to content type string.
* @return Content type string
*/
@Override
public String toString();
}HTTP header name constants and utilities with case-insensitive comparison.
/**
* Represents HTTP header names for multiple versions of HTTP.
*/
class HttpHeaderName extends ExpandableStringEnum<HttpHeaderName> {
/**
* Gets or creates the HttpHeaderName for the passed name.
* @param name The name
* @return The HttpHeaderName of the passed name, or null if name was null
*/
public static HttpHeaderName fromString(String name);
/**
* Gets the HTTP header name based on the name passed into fromString.
* @return The HTTP header name based on the construction of this HttpHeaderName
*/
public String getCaseSensitiveName();
/**
* Gets the HTTP header name lower cased.
* @return The HTTP header name lower cased
*/
public String getCaseInsensitiveName();
// Common header name constants
public static final HttpHeaderName ACCEPT = fromString("Accept");
public static final HttpHeaderName AUTHORIZATION = fromString("Authorization");
public static final HttpHeaderName CONTENT_TYPE = fromString("Content-Type");
public static final HttpHeaderName CONTENT_LENGTH = fromString("Content-Length");
public static final HttpHeaderName USER_AGENT = fromString("User-Agent");
public static final HttpHeaderName CACHE_CONTROL = fromString("Cache-Control");
public static final HttpHeaderName ETAG = fromString("ETag");
public static final HttpHeaderName IF_MATCH = fromString("If-Match");
public static final HttpHeaderName IF_NONE_MATCH = fromString("If-None-Match");
public static final HttpHeaderName LAST_MODIFIED = fromString("Last-Modified");
public static final HttpHeaderName LOCATION = fromString("Location");
public static final HttpHeaderName RETRY_AFTER = fromString("Retry-After");
public static final HttpHeaderName DATE = fromString("Date");
public static final HttpHeaderName HOST = fromString("Host");
public static final HttpHeaderName X_MS_CLIENT_REQUEST_ID = fromString("x-ms-client-request-id");
public static final HttpHeaderName X_MS_REQUEST_ID = fromString("x-ms-request-id");
public static final HttpHeaderName TRACEPARENT = fromString("traceparent");
}Factory for creating REST API clients from annotated interfaces.
/**
* Type to create a proxy implementation for an interface describing REST API methods.
*/
class RestProxy {
/**
* Create a proxy implementation of the provided Swagger interface.
* @param <A> Swagger interface type
* @param swaggerInterface Swagger interface to implement
* @param httpPipeline HTTP pipeline for requests
* @return Proxy implementation
*/
public static <A> A create(Class<A> swaggerInterface, HttpPipeline httpPipeline);
/**
* Create a proxy implementation of the provided Swagger interface.
* @param <A> Swagger interface type
* @param swaggerInterface Swagger interface to implement
* @param httpPipeline HTTP pipeline for requests
* @param serializer Serializer for request/response bodies
* @return Proxy implementation
*/
public static <A> A create(Class<A> swaggerInterface, HttpPipeline httpPipeline,
SerializerAdapter serializer);
}import com.azure.core.http.*;
import com.azure.core.http.policy.*;
import com.azure.core.credential.TokenCredential;
// Create pipeline with authentication and retry
TokenCredential credential = // ... get credential
HttpPipeline pipeline = new HttpPipelineBuilder()
.httpClient(HttpClient.createDefault())
.policies(
new UserAgentPolicy("MyApp/1.0"),
new BearerTokenAuthenticationPolicy(credential, "https://vault.azure.net/.default"),
new RetryPolicy(new RetryOptions()),
new HttpLoggingPolicy(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC))
)
.build();import com.azure.core.http.*;
import com.azure.core.util.BinaryData;
import com.azure.core.util.Context;
// Create and configure request
HttpRequest request = new HttpRequest(HttpMethod.POST, "https://api.example.com/users");
request.setHeader("Content-Type", "application/json");
request.setHeader("Accept", "application/json");
// Set JSON body
String jsonBody = "{\"name\":\"John\",\"email\":\"john@example.com\"}";
request.setBody(BinaryData.fromString(jsonBody));
// Send request through pipeline
HttpResponse response = pipeline.send(request, Context.NONE).block();
// Handle response
if (response.getStatusCode() == 201) {
BinaryData responseBody = response.getBodyAsBinaryData().block();
String responseJson = responseBody.toString();
System.out.println("Created user: " + responseJson);
} else {
System.out.println("Request failed with status: " + response.getStatusCode());
}import com.azure.core.annotation.*;
import com.azure.core.http.rest.Response;
import com.azure.core.util.Context;
import reactor.core.publisher.Mono;
// Define REST API interface
@ServiceInterface(name = "UserService")
interface UserServiceClient {
@Get("/users/{userId}")
@ExpectedResponses({200})
Mono<Response<User>> getUser(@PathParam("userId") String userId);
@Post("/users")
@ExpectedResponses({201})
Mono<Response<User>> createUser(@BodyParam("application/json") User user);
}
// Create client using REST proxy
UserServiceClient client = RestProxy.create(UserServiceClient.class, pipeline);
// Use the client
User newUser = new User("John", "john@example.com");
Response<User> response = client.createUser(newUser).block();
User createdUser = response.getValue();Install with Tessl CLI
npx tessl i tessl/maven-com-azure--azure-core