7.1.2 Posting a Request Body
All the examples so far have used the same HTTP method i.e GET
. The HttpRequest interface has factory methods for all the different HTTP methods. The following table summarizes them:
Method | Description | Allows Body |
---|---|---|
Constructs an HTTP |
| |
Constructs an HTTP |
| |
Constructs an HTTP |
| |
Constructs an HTTP |
| |
Constructs an HTTP |
| |
Constructs an HTTP |
| |
Constructs an HTTP |
|
A create
method also exists to construct a request for any HttpMethod type. Since the POST
, PUT
and PATCH
methods require a body, a second argument which is the body object is required.
The following example demonstrates how to send a simple String
body:
Sending a String
body
Flux<HttpResponse<String>> call = Flux.from(client.exchange(
POST("/hello", "Hello John") (1)
.contentType(MediaType.TEXT_PLAIN_TYPE)
.accept(MediaType.TEXT_PLAIN_TYPE), (2)
String.class (3)
));
Sending a String
body
Flux<HttpResponse<String>> call = Flux.from(client.exchange(
POST("/hello", "Hello John") (1)
.contentType(MediaType.TEXT_PLAIN_TYPE)
.accept(MediaType.TEXT_PLAIN_TYPE), (2)
String (3)
))
Sending a String
body
val call = client.exchange(
POST("/hello", "Hello John") (1)
.contentType(MediaType.TEXT_PLAIN_TYPE)
.accept(MediaType.TEXT_PLAIN_TYPE), String::class.java (3)
)
1 | The POST method is used; the first argument is the URI and the second is the body |
2 | The content type and accepted type are set to text/plain (the default is application/json ) |
3 | The expected response type is a String |