Adding body to HttpRequest

Available

3.1 Body Publishers

I hope you have not forgotten that in addition to GET requests , there are also PUT and POST requests , when you also need to add to the request request body, that is, the request body.

HttpRequestThe class has a special inner class for this BodyPublisher. Although it is technically an interface that has multiple implementations, which we will discuss below

And we will start with the simplest - the absence of a request body. Yes, it happens.

HttpRequest request = HttpRequest.newBuilder()
   .uri(new URI("https://codegym.cc"))
   .POST(HttpRequest.BodyPublishers.noBody())
   .build();

Simple and beautiful.

3.2 ofString()

The second most common option is to pass some string as the request body. This is done very simply:

HttpRequest request = HttpRequest.newBuilder()
   .uri(new URI("https://codegym.cc"))
   .headers("Content-Type", "text/plain;charset=UTF-8")
   .POST(HttpRequest.BodyPublishers.ofString("Hello"))
   .build();

By the way, you can set the encoding of the transmitted string. It can be very useful if the http server to which the request is sent does not work on UTF8.

HttpRequest request = HttpRequest.newBuilder()
   .uri(new URI("https://codegym.cc"))
   .POST(HttpRequest.BodyPublishers.ofString("Hello", Charset. US-ASCII)))
   .build();

3.3 ofFile()

Finally, you may want to attach a file to the POST request . This is how your avatars are usually uploaded to the server. To do this, you need to call the method ofFile(), where to transfer Paththe local file:

Path avatar = Path.of("c://avatar.jpeg");

HttpRequest request = HttpRequest.newBuilder()
   .uri(new URI("https://codegym.cc"))
   .headers("Content-Type", "image/jpeg")
   .POST(HttpRequest.BodyPublishers.ofFile(avatar))
   .build();

3.4 ofByteArray()

Another common scenario is to send a set of bytes to the server. For example, you serialized some object as a set of bytes, encrypted something, or just want to send some data buffer. This requires a method .ofByteArray().

This method takes an array of bytes as a parameter. Example:

byte[] data = "My Secret Message".getBytes();
byte[] dataEncripted = SuperEncriptor.encript(data);

HttpRequest request = HttpRequest.newBuilder()
   .uri(new URI("https://codegym.cc"))
   .headers("Content-Type", "application/octet-stream")
   .POST(HttpRequest.BodyPublishers.ofByteArray(dataEncripted))
   .build();

3.5 ofInputStream()

Finally, the last but not least interesting scenario is attaching InputStreamto a POST request .

There is a special function for this ofInputStream(). And she is very smart. It allows you to organize the transfer of data from stream to stream, attach a data stream to a POS request that is not even open yet.

You need to pass a function to the function ofInputStream(), which will return a stream as a result InputStream.

Example:

byte[] data = "My Secret Message".getBytes();
//wrapping an array of bytes into a stream.
InputStream is = new ByteArrayInputStream(data);

HttpRequest request = HttpRequest.newBuilder()
   .uri(new URI("https://codegym.cc"))
   .headers("Content-Type", "application/octet-stream")
   .POST(HttpRequest.BodyPublishers.ofInputStream (() -> is;))
   .build();

I didn’t bother to fence the garden here, but I think you understand the principle. Why did they do that? You can almost always use the ofByteArray(). But if you want to send data asynchronously or you need to implement some particularly complex scenario, then you can use this method.

Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet