@Immutable
public interface Request
Instance of this class is supposed to be used this way:
String name = new ApacheRequest("https://www.example.com:8080") .uri().path("/users").queryParam("id", 333).back() .method(Request.GET) .header(HttpHeaders.ACCEPT, MediaType.TEXT_XML) .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .assertXPath("/page/links/link[@rel='see']") .rel("/page/links/link[@rel='see']/@href") .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON) .fetch() .as(JsonResponse.class) .json().getJsonObject().getString("name");
Since version 0.10 it is recommended to use
RetryWire
decorator to avoid accidental IOException
when connection is weak
or unstable, for example:
String body = new JdkRequest("https://www.google.com") .through(RetryWire.class) .fetch() .body();
Instances of this interface are immutable and thread-safe.
You can use either ApacheRequest or JdkRequest,
according to your needs. JdkRequest won't require any additional
dependencies, while ApacheRequest will properly support all
possible HTTP methods (JdkRequest doesn't support PATCH
,
for example).
JdkRequest
,
ApacheRequest
Modifier and Type | Field and Description |
---|---|
static String |
DELETE
DELETE method name.
|
static String |
GET
GET method name.
|
static String |
HEAD
HEAD method name.
|
static String |
OPTIONS
OPTIONS method name.
|
static String |
PATCH
PATCH method name.
|
static String |
POST
POST method name.
|
static String |
PUT
PUT method name.
|
Modifier and Type | Method and Description |
---|---|
RequestBody |
body()
Get request body.
|
Response |
fetch()
Execute it with a specified HTTP method.
|
Response |
fetch(InputStream stream)
Execute this request using the content provided by the
InputStream being passed as the request body. |
Request |
header(String name,
Object value)
Set request header.
|
Request |
method(String method)
Use this method.
|
RequestBody |
multipartBody()
Get multipart form (multipart/form-data) body.
|
Request |
reset(String name)
Remove all headers with this name.
|
<T extends Wire> |
through(Class<T> type,
Object... args)
Send it through a decorating
Wire . |
Request |
through(Wire wire)
Send it through a decorating
Wire . |
Request |
timeout(int connect,
int read)
Use this timeout values.
|
RequestURI |
uri()
Get destination URI.
|
static final String GET
static final String POST
static final String PUT
static final String HEAD
static final String DELETE
static final String OPTIONS
static final String PATCH
RequestURI uri()
RequestBody body()
RequestBody multipartBody()
Request header(String name, Object value)
name
- ImmutableHeader namevalue
- Value of the header to setRequest reset(String name)
name
- ImmutableHeader nameRequest method(String method)
method
- The method to useRequest timeout(int connect, int read)
connect
- The connect timeout to use in msread
- The read timeout to use in msResponse fetch() throws IOException
IOException
- If fails to fetch HTTP requestResponse fetch(InputStream stream) throws IOException
InputStream
being passed as the request body. Note that the
request MUST have an empty body when this method is being invoked, or
it will throw an IllegalStateException
.stream
- The input stream to useIOException
- If fails to fetch HTTP request<T extends Wire> Request through(Class<T> type, Object... args)
Wire
.T
- Type to usetype
- Type of wire to useargs
- Optional arguments for the wire constructorCopyright © 2012–2020 jcabi.com. All rights reserved.