7.1.2 Posting a Request Body
All the examples up until now 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 the available methods:
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 simply String
body:
Sending a String
body
Flowable<HttpResponse<String>> call = 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
Flowable<HttpResponse<String>> call = 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
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 with the first argument being the URI and the second argument the body |
2 | The content type and accepted type are set to text/plain (the default content type is application/json ) |
3 | The expected response type is a String |