Package com.jcabi.http
Interface Request
-
- All Known Implementing Classes:
ApacheRequest
,BaseRequest
,FakeRequest
,JdkRequest
@Immutable public interface Request
RESTful 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 accidentalIOException
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).- Since:
- 0.8
- See Also:
JdkRequest
,ApacheRequest
-
-
Field Summary
Fields Modifier and Type Field 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.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method 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 theInputStream
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.Request
through(Wire wire)
Send it through a decoratingWire
.<T extends Wire>
Requestthrough(Class<T> type, Object... args)
Send it through a decoratingWire
.Request
timeout(int connect, int read)
Use this timeout values.RequestURI
uri()
Get destination URI.
-
-
-
Field Detail
-
GET
static final String GET
GET method name.- See Also:
- Constant Field Values
-
POST
static final String POST
POST method name.- See Also:
- Constant Field Values
-
PUT
static final String PUT
PUT method name.- See Also:
- Constant Field Values
-
HEAD
static final String HEAD
HEAD method name.- See Also:
- Constant Field Values
-
DELETE
static final String DELETE
DELETE method name.- See Also:
- Constant Field Values
-
OPTIONS
static final String OPTIONS
OPTIONS method name.- See Also:
- Constant Field Values
-
PATCH
static final String PATCH
PATCH method name.- See Also:
- Constant Field Values
-
-
Method Detail
-
uri
RequestURI uri()
Get destination URI.- Returns:
- The destination it is currently pointing to
-
body
RequestBody body()
Get request body.- Returns:
- New alternated request
-
multipartBody
RequestBody multipartBody()
Get multipart form (multipart/form-data) body.- Returns:
- New altered request
-
header
Request header(String name, Object value)
Set request header.- Parameters:
name
- ImmutableHeader namevalue
- Value of the header to set- Returns:
- New alternated request
-
reset
Request reset(String name)
Remove all headers with this name.- Parameters:
name
- ImmutableHeader name- Returns:
- New alternated request
- Since:
- 0.10
-
method
Request method(String method)
Use this method.- Parameters:
method
- The method to use- Returns:
- New alternated request
-
timeout
Request timeout(int connect, int read)
Use this timeout values.- Parameters:
connect
- The connect timeout to use in msread
- The read timeout to use in ms- Returns:
- New alternated request
-
fetch
Response fetch() throws IOException
Execute it with a specified HTTP method.- Returns:
- Response
- Throws:
IOException
- If fails to fetch HTTP request
-
fetch
Response fetch(InputStream stream) throws IOException
Execute this request using the content provided by theInputStream
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 anIllegalStateException
.- Parameters:
stream
- The input stream to use- Returns:
- Response
- Throws:
IOException
- If fails to fetch HTTP request- Since:
- 1.8
-
through
<T extends Wire> Request through(Class<T> type, Object... args)
Send it through a decoratingWire
.- Type Parameters:
T
- Type to use- Parameters:
type
- Type of wire to useargs
- Optional arguments for the wire constructor- Returns:
- New request with a wire decorated
- Since:
- 0.10
-
-