0
# HTTP Request Methods
1
2
Apache HttpClient provides specific implementation classes for all standard HTTP methods. Each method class handles the appropriate HTTP semantics and provides method-specific functionality.
3
4
## Base Classes
5
6
### HttpUriRequest Interface
7
8
```java { .api }
9
public interface HttpUriRequest extends HttpRequest {
10
String getMethod();
11
URI getURI();
12
void abort() throws UnsupportedOperationException;
13
}
14
```
15
16
Base interface for HTTP requests with URI support.
17
18
### HttpRequestBase
19
20
```java { .api }
21
public abstract class HttpRequestBase implements HttpUriRequest, AbortableHttpRequest, Cloneable, Configurable {
22
public void setURI(URI uri);
23
public URI getURI();
24
public void setConfig(RequestConfig config);
25
public RequestConfig getConfig();
26
public void abort();
27
public boolean isAborted();
28
}
29
```
30
31
Abstract base class for HTTP requests providing common functionality.
32
33
### HttpEntityEnclosingRequestBase
34
35
```java { .api }
36
public abstract class HttpEntityEnclosingRequestBase extends HttpRequestBase implements HttpEntityEnclosingRequest {
37
public void setEntity(HttpEntity entity);
38
public HttpEntity getEntity();
39
public boolean expectContinue();
40
}
41
```
42
43
Base class for HTTP requests that can contain an entity body (POST, PUT, PATCH).
44
45
## HTTP Method Implementations
46
47
### GET Requests
48
49
```java { .api }
50
public class HttpGet extends HttpRequestBase {
51
public HttpGet();
52
public HttpGet(URI uri);
53
public HttpGet(String uri);
54
public String getMethod();
55
}
56
```
57
58
HTTP GET method implementation for retrieving resources.
59
60
```java
61
HttpGet httpGet = new HttpGet("https://api.example.com/users");
62
CloseableHttpResponse response = httpClient.execute(httpGet);
63
```
64
65
### POST Requests
66
67
```java { .api }
68
public class HttpPost extends HttpEntityEnclosingRequestBase {
69
public HttpPost();
70
public HttpPost(URI uri);
71
public HttpPost(String uri);
72
public String getMethod();
73
}
74
```
75
76
HTTP POST method implementation for submitting data.
77
78
```java
79
HttpPost httpPost = new HttpPost("https://api.example.com/users");
80
StringEntity entity = new StringEntity("{\"name\":\"John\",\"age\":30}", ContentType.APPLICATION_JSON);
81
httpPost.setEntity(entity);
82
CloseableHttpResponse response = httpClient.execute(httpPost);
83
```
84
85
### PUT Requests
86
87
```java { .api }
88
public class HttpPut extends HttpEntityEnclosingRequestBase {
89
public HttpPut();
90
public HttpPut(URI uri);
91
public HttpPut(String uri);
92
public String getMethod();
93
}
94
```
95
96
HTTP PUT method implementation for updating resources.
97
98
```java
99
HttpPut httpPut = new HttpPut("https://api.example.com/users/123");
100
StringEntity entity = new StringEntity("{\"name\":\"John Updated\"}");
101
httpPut.setEntity(entity);
102
CloseableHttpResponse response = httpClient.execute(httpPut);
103
```
104
105
### DELETE Requests
106
107
```java { .api }
108
public class HttpDelete extends HttpRequestBase {
109
public HttpDelete();
110
public HttpDelete(URI uri);
111
public HttpDelete(String uri);
112
public String getMethod();
113
}
114
```
115
116
HTTP DELETE method implementation for removing resources.
117
118
```java
119
HttpDelete httpDelete = new HttpDelete("https://api.example.com/users/123");
120
CloseableHttpResponse response = httpClient.execute(httpDelete);
121
```
122
123
### HEAD Requests
124
125
```java { .api }
126
public class HttpHead extends HttpRequestBase {
127
public HttpHead();
128
public HttpHead(URI uri);
129
public HttpHead(String uri);
130
public String getMethod();
131
}
132
```
133
134
HTTP HEAD method implementation for retrieving headers only.
135
136
```java
137
HttpHead httpHead = new HttpHead("https://api.example.com/users/123");
138
CloseableHttpResponse response = httpClient.execute(httpHead);
139
```
140
141
### OPTIONS Requests
142
143
```java { .api }
144
public class HttpOptions extends HttpRequestBase {
145
public HttpOptions();
146
public HttpOptions(URI uri);
147
public HttpOptions(String uri);
148
public String getMethod();
149
public Set<String> getAllowedMethods(HttpResponse response);
150
}
151
```
152
153
HTTP OPTIONS method implementation for discovering allowed methods.
154
155
```java
156
HttpOptions httpOptions = new HttpOptions("https://api.example.com/users");
157
CloseableHttpResponse response = httpClient.execute(httpOptions);
158
Set<String> allowedMethods = httpOptions.getAllowedMethods(response);
159
```
160
161
### TRACE Requests
162
163
```java { .api }
164
public class HttpTrace extends HttpRequestBase {
165
public HttpTrace();
166
public HttpTrace(URI uri);
167
public HttpTrace(String uri);
168
public String getMethod();
169
}
170
```
171
172
HTTP TRACE method implementation for diagnostic purposes.
173
174
```java
175
HttpTrace httpTrace = new HttpTrace("https://api.example.com");
176
CloseableHttpResponse response = httpClient.execute(httpTrace);
177
```
178
179
### PATCH Requests
180
181
```java { .api }
182
public class HttpPatch extends HttpEntityEnclosingRequestBase {
183
public HttpPatch();
184
public HttpPatch(URI uri);
185
public HttpPatch(String uri);
186
public String getMethod();
187
}
188
```
189
190
HTTP PATCH method implementation for partial resource updates.
191
192
```java
193
HttpPatch httpPatch = new HttpPatch("https://api.example.com/users/123");
194
StringEntity entity = new StringEntity("{\"age\":31}");
195
httpPatch.setEntity(entity);
196
CloseableHttpResponse response = httpClient.execute(httpPatch);
197
```
198
199
## Request Builder
200
201
### RequestBuilder Class
202
203
```java { .api }
204
public class RequestBuilder {
205
public static RequestBuilder create(String method);
206
public static RequestBuilder get();
207
public static RequestBuilder get(URI uri);
208
public static RequestBuilder get(String uri);
209
public static RequestBuilder head();
210
public static RequestBuilder head(URI uri);
211
public static RequestBuilder head(String uri);
212
public static RequestBuilder patch();
213
public static RequestBuilder patch(URI uri);
214
public static RequestBuilder patch(String uri);
215
public static RequestBuilder post();
216
public static RequestBuilder post(URI uri);
217
public static RequestBuilder post(String uri);
218
public static RequestBuilder put();
219
public static RequestBuilder put(URI uri);
220
public static RequestBuilder put(String uri);
221
public static RequestBuilder delete();
222
public static RequestBuilder delete(URI uri);
223
public static RequestBuilder delete(String uri);
224
public static RequestBuilder trace();
225
public static RequestBuilder trace(URI uri);
226
public static RequestBuilder trace(String uri);
227
public static RequestBuilder options();
228
public static RequestBuilder options(URI uri);
229
public static RequestBuilder options(String uri);
230
}
231
```
232
233
### RequestBuilder Configuration Methods
234
235
```java { .api }
236
public RequestBuilder setUri(URI uri);
237
public RequestBuilder setUri(String uri);
238
public RequestBuilder setHeader(Header header);
239
public RequestBuilder setHeader(String name, String value);
240
public RequestBuilder addHeader(Header header);
241
public RequestBuilder addHeader(String name, String value);
242
public RequestBuilder removeHeader(Header header);
243
public RequestBuilder removeHeaders(String name);
244
public RequestBuilder setEntity(HttpEntity entity);
245
public RequestBuilder addParameter(String name, String value);
246
public RequestBuilder addParameter(NameValuePair nvp);
247
public RequestBuilder addParameters(NameValuePair... nvps);
248
public RequestBuilder setCharset(Charset charset);
249
public RequestBuilder setConfig(RequestConfig config);
250
public HttpUriRequest build();
251
```
252
253
Example usage:
254
255
```java
256
HttpUriRequest request = RequestBuilder.post()
257
.setUri("https://api.example.com/users")
258
.setHeader("Content-Type", "application/json")
259
.setEntity(new StringEntity("{\"name\":\"John\"}"))
260
.build();
261
262
CloseableHttpResponse response = httpClient.execute(request);
263
```
264
265
## Request Configuration
266
267
### Configurable Interface
268
269
```java { .api }
270
public interface Configurable {
271
RequestConfig getConfig();
272
}
273
```
274
275
Interface for objects that can be configured with RequestConfig.
276
277
### Setting Request Configuration
278
279
```java
280
RequestConfig config = RequestConfig.custom()
281
.setSocketTimeout(10000)
282
.setConnectTimeout(10000)
283
.build();
284
285
HttpGet httpGet = new HttpGet("https://api.example.com");
286
httpGet.setConfig(config);
287
```
288
289
## Request Execution Control
290
291
### AbortableHttpRequest Interface
292
293
```java { .api }
294
public interface AbortableHttpRequest {
295
void abort() throws UnsupportedOperationException;
296
}
297
```
298
299
Interface for requests that can be aborted.
300
301
### HttpExecutionAware Interface
302
303
```java { .api }
304
public interface HttpExecutionAware {
305
boolean isAborted();
306
void setCancellable(Cancellable cancellable);
307
}
308
```
309
310
Interface for execution-aware requests that can be cancelled.
311
312
## Types
313
314
### HttpEntity
315
316
```java { .api }
317
public interface HttpEntity {
318
boolean isRepeatable();
319
boolean isChunked();
320
long getContentLength();
321
Header getContentType();
322
Header getContentEncoding();
323
InputStream getContent() throws IOException, UnsupportedOperationException;
324
void writeTo(OutputStream outstream) throws IOException;
325
boolean isStreaming();
326
void consumeContent() throws IOException;
327
}
328
```
329
330
Interface representing an HTTP entity (request or response body).
331
332
### Common Entity Implementations
333
334
```java { .api }
335
public class StringEntity implements HttpEntity;
336
public class ByteArrayEntity implements HttpEntity;
337
public class FileEntity implements HttpEntity;
338
public class InputStreamEntity implements HttpEntity;
339
public class UrlEncodedFormEntity implements HttpEntity;
340
```
341
342
Common implementations of HttpEntity for different data types.
343
344
### EntityBuilder
345
346
```java { .api }
347
public class EntityBuilder {
348
public static EntityBuilder create();
349
public EntityBuilder setText(String text);
350
public EntityBuilder setBinary(byte[] binary);
351
public EntityBuilder setStream(InputStream stream);
352
public EntityBuilder setParameters(List<NameValuePair> parameters);
353
public EntityBuilder setParameters(NameValuePair... parameters);
354
public EntityBuilder setFile(File file);
355
public EntityBuilder setContentType(ContentType contentType);
356
public EntityBuilder setContentEncoding(String contentEncoding);
357
public EntityBuilder chunked();
358
public EntityBuilder gzipCompress();
359
public HttpEntity build();
360
}
361
```
362
363
Builder for creating HTTP entities with different content types and encoding options.
364
365
```java
366
HttpEntity entity = EntityBuilder.create()
367
.setText("{\"name\":\"John\",\"age\":30}")
368
.setContentType(ContentType.APPLICATION_JSON)
369
.build();
370
371
HttpPost httpPost = new HttpPost("https://api.example.com/users");
372
httpPost.setEntity(entity);
373
```