{title:'HTTP Entities and Resources', created:'9.0.0'}

The {@link oaj.http.entity} package contains implementations of org.apache.http.HttpEntity. These are entities that can be sent or received with an HTTP message. They consist of a message body and the headers Content-Type, Content-Length, and Content-Encoding.

The {@link oaj.http.resource} package contains implementations of {@link oaj.http.resource.HttpResource} which are extensions of org.apache.http.HttpEntity with arbitrary headers added beyond the standard content headers.

- {@code org.apache.http.HttpEntity}
  - {@link oaj.http.entity.BasicHttpEntity}
    - {@link oaj.http.entity.ByteArrayEntity}
    - {@link oaj.http.entity.FileEntity}
    - {@link oaj.http.entity.StreamEntity}
    - {@link oaj.http.entity.ReaderEntity}
    - {@link oaj.http.entity.SerializedEntity}
    - {@link oaj.http.entity.StringEntity}
  - {@link oaj.http.resource.HttpResource}
    - {@link oaj.http.resource.BasicResource}
      - {@link oaj.http.resource.ByteArrayResource}
      - {@link oaj.http.resource.FileResource}
      - {@link oaj.http.resource.StreamResource}
      - {@link oaj.http.resource.ReaderResource}
      - {@link oaj.http.resource.StringResource}

##### Example

| import static org.apache.juneau.http.HttpEntities.*; | | byte[] payload = {...}; | | // Create via type builder. | HttpEntity entity = ByteArrayEntity | .create() | .content(payload) | .contentType(ContentType.APPLICATION_OCTET_STREAM); | | // Create via HttpEntities. | HttpEntity entity = byteArrayEntity(payload, ContentType.APPLICATION_OCTET_STREAM);

HTTP entities and resources can be used by both the server and client side APIs described in later sections.

##### Server-side example:

| // REST endpoint that simply echos an HTTP entity. | @RestPost(path="/entity") | public HttpEntity echoMyEntity(HttpEntity entity) { | return entity; | } | | // REST endpoint that serves up a static file. | @RestGet(path="/resource/{fileName}") | public HttpResource getStaticFile(@Path String fileName, Locale locale) { | getContext().getStaticFiles().resolve(fileName, locale).orElseThrow(NotFound::new); | }

##### Client-side example:

| // REST client that uses the echo REST endpoint above. | | HttpEntity entity = byteArrayEntity(...); | | entity = RestClient.create() | .build() | .rootUrl(URI) | .post("/entity", entity) | .run() | .assertStatus().asCode().is(200) | .getContent().as(ByteArrayEntity.class);