Skip to main content

Request Content

The request body can either be passed in with the client creator method (e.g. post(uri,body)), or can be specified via the following methods:

RestRequestcontent(Object)content(Object,HttpPartSchema)

The request body can be any of the following types:

Object - POJO to be converted to text using the Serializer defined on the client or request.Reader - Raw contents of Reader will be serialized to remote resource.InputStream - Raw contents of InputStream will be serialized to remote resource.HttpEntity - Bypass Juneau serialization and pass HttpEntity directly to HttpClient.PartList - Converted to a URL-encoded FORM post.Supplier - A supplier of anything on this list.
Examples
// Create a client with JSON 5 support.
RestClient client = RestClient.create().json5().build();

// Post a JSON-serialized bean.
client
.post(URI)
.content(bean)
.complete()
.assertStatus().asCode().is(200);

// Post contents from a reader.
client
.post(URI)
.content(new FileReader("/tmp/foo.json"))
.complete()
.assertStatus().asCode().is(200);

// Post contents from an Apache HttpEntity object.
client
.post(URI)
.content(new StringEntity(jsonString, ContentType.APPLICATION_JSON))
.complete()
.assertStatus().asCode().is(200);
note

If the serializer on the client or request is explicitly set to null, POJOs will be converted to strings using the registered part serializer as content type "text/plain.

If the part serializer is also null, POJOs will be converted to strings using ClassMeta.toString(Object) which typically just calls Object.toString().