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 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).
- Since:
- 0.8
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDELETE method name.static final StringGET method name.static final StringHEAD method name.static final StringOPTIONS method name.static final StringPATCH method name.static final StringPOST method name.static final StringPUT method name. -
Method Summary
Modifier and TypeMethodDescriptionbody()Get request body.fetch()Execute it with a specified HTTP method.fetch(InputStream stream) Execute this request using the content provided by theInputStreambeing passed as the request body.Set request header.Use this method.Get multipart form (multipart/form-data) body.Remove all headers with this name.Send it through a decoratingWire.Send it through a decoratingWire.timeout(int connect, int read) Use this timeout values.uri()Get destination URI.
-
Field Details
-
GET
GET method name.- See Also:
-
POST
POST method name.- See Also:
-
PUT
PUT method name.- See Also:
-
HEAD
HEAD method name.- See Also:
-
DELETE
DELETE method name.- See Also:
-
OPTIONS
OPTIONS method name.- See Also:
-
PATCH
PATCH method name.- See Also:
-
-
Method Details
-
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
Set request header.- Parameters:
name- ImmutableHeader namevalue- Value of the header to set- Returns:
- New alternated request
-
reset
Remove all headers with this name.- Parameters:
name- ImmutableHeader name- Returns:
- New alternated request
- Since:
- 0.10
-
method
Use this method.- Parameters:
method- The method to use- Returns:
- New alternated request
-
timeout
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
Execute it with a specified HTTP method.- Returns:
- Response
- Throws:
IOException- If fails to fetch HTTP request
-
fetch
Execute this request using the content provided by theInputStreambeing 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
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
-
through
Send it through a decoratingWire.- Parameters:
wire- Wire to use- Returns:
- New request with a wire decorated
- Since:
- 0.10
-