{title:'HTTP Parts', created:'9.0.0'}

The {@link oaj.http.part} package contains implementations of org.apache.http.NameValuePair to be used for query/form-data/path parts and part lists.

- {@code org.apache.http.NameValuePair}
  - {@link oaj.http.part.BasicPart}
    - {@link oaj.http.part.BasicBooleanPart}
    - {@link oaj.http.part.BasicCsvArrayPart}
    - {@link oaj.http.part.BasicDatePart}
    - {@link oaj.http.part.BasicIntegerPart}
    - {@link oaj.http.part.BasicLongPart}
    - {@link oaj.http.part.BasicPartIterator}
    - {@link oaj.http.part.BasicStringPart}
    - {@link oaj.http.part.BasicUriPart}

##### PartList

The {@link oaj.http.part.PartList} class is a list of HTTP parts (form-data, query-parameters, path-parameters).

##### Example

| PartList parts = PartList | .create() | .append(MyPart. of("foo")) | .append("Bar", ()->getDynamicValueFromSomewhere());

Convenience creators are provided for creating lists with minimal code:

| PartList parts = PartList. of(BasicIntegerPart. of("foo", 1));

Static methods are provided on {@link oaj.http.HttpParts} to further simplify creation of part lists.

| import static org.apache.juneau.http.HttpParts.*; | | PartList parts = partList(integerPart("foo", 1), booleanPart("bar", false));

The builder class supports setting default part values (i.e. add a part to the list if it isn't otherwise in the list). Note that this is different from simply setting a value twice as using default values will not overwrite existing parts.
The following example notes the distinction:

| parts = PartList | .create() | .set("Foo", "bar") | .set("Foo", "baz"); | assertObject(parts).isString("foo=baz"); | | parts = PartList | .create() | .set("Foo", "bar") | .setDefault("Foo", "baz"); | assertObject(parts).isString("foo=bar");

Various methods are provided for iterating over the parts in this list to avoid array copies.

- {@link oaj.http.part.PartList#forEach(Consumer) forEach(Consumer)} / {@link oaj.http.part.PartList#forEach(String,Consumer) forEach(String,Consumer)} / {@link oaj.http.part.PartList#forEach(Predicate,Consumer) forEach(Predicate,Consumer)} - Use consumers to process parts.
- {@link oaj.http.part.PartList#partIterator() partIterator()} / {@link oaj.http.part.PartList#partIterator(String) partIterator(String)} - Use an {@link oaj.http.part.PartIterator} to process parts.
- {@link oaj.http.part.PartList#stream() stream()} / {@link oaj.http.part.PartList#stream(String) stream(String)} - Use a stream.

In general, try to use these over the {@link oaj.http.part.PartList#getAll() getAll()} / {@link oaj.http.part.PartList#getAll(String) getAll(String)} methods that require array copies.

Similar to the way multiple headers can be collapsed into a single value, the {@link oaj.http.part.PartList#get(String) get(String)} method is special in that it will collapse multiple parts with the same name into a single comma-delimited list.

The {@link oaj.http.part.PartList#get(Class) get(Class)} and {@link oaj.http.part.PartList#get(String,Class) get(String,Class)} methods are provided for working with {@link oaj.http.annotation.FormData} / {@link oaj.http.annotation.Query} / {@link oaj.http.annotation.Path}-annotated beans.

##### Example

| MyQueryBean foo = parts.get(MyQueryBean. class);

A {@link oaj.svl.VarResolver} can be associated with this builder to create part values with embedded variables that are resolved at runtime.

##### Example

| // Create a part list with dynamically-resolving values pulled from a system property. | | System. setProperty("foo", "bar"); | | PartList parts = PartList | .create() | .resolving() | .append("X1", "$S{foo}") | .append("X2", ()->"$S{foo}"); | | assertObject(parts).isString("X1=bar&X2=bar");

The {@link oaj.http.part.PartList} object can be extended to defined pre-packaged lists of parts which can be used in various annotations throughout the framework.

##### Example

| // A predefined list of parts. | public class MyPartList extends PartList { | public MyPartList() { | super(BasicIntegerPart. of("foo",1), BasicBooleanPart. of("bar",false)); | } | }