0
# HTTP Client and Server
1
2
Full-featured HTTP 1.1/2.0 client and server implementation with WebSocket support, request/response handling, streaming capabilities, and comprehensive configuration options.
3
4
## Capabilities
5
6
### HTTP Server Creation
7
8
Create and configure HTTP servers for handling incoming requests.
9
10
```java { .api }
11
/**
12
* Create an HTTP server with default options
13
* @return HttpServer instance
14
*/
15
HttpServer createHttpServer();
16
17
/**
18
* Create an HTTP server with custom options
19
* @param options Server configuration options
20
* @return HttpServer instance
21
*/
22
HttpServer createHttpServer(HttpServerOptions options);
23
24
/**
25
* HTTP Server interface for handling requests
26
*/
27
interface HttpServer extends Measured, Closeable {
28
/**
29
* Set the request handler for all HTTP requests
30
* @param handler Handler to process incoming requests
31
* @return this for chaining
32
*/
33
HttpServer requestHandler(Handler<HttpServerRequest> handler);
34
35
/**
36
* Set the WebSocket handler for WebSocket upgrade requests
37
* @param handler Handler to process WebSocket connections
38
* @return this for chaining
39
*/
40
HttpServer webSocketHandler(Handler<ServerWebSocket> handler);
41
42
/**
43
* Start listening on a port
44
* @param port Port to listen on
45
* @return Future that completes when server is listening
46
*/
47
Future<HttpServer> listen(int port);
48
49
/**
50
* Start listening on a specific host and port
51
* @param port Port to listen on
52
* @param host Host to bind to
53
* @return Future that completes when server is listening
54
*/
55
Future<HttpServer> listen(int port, String host);
56
57
/**
58
* Start listening on a SocketAddress
59
* @param address Address to listen on
60
* @return Future that completes when server is listening
61
*/
62
Future<HttpServer> listen(SocketAddress address);
63
64
/**
65
* Get the actual port the server is listening on
66
* @return The port number
67
*/
68
int actualPort();
69
70
/**
71
* Close the server
72
* @return Future that completes when server is closed
73
*/
74
Future<Void> close();
75
76
/**
77
* Set connection handler
78
* @param handler Handler for new connections
79
* @return this for chaining
80
*/
81
HttpServer connectionHandler(Handler<HttpConnection> handler);
82
83
/**
84
* Set exception handler
85
* @param handler Exception handler
86
* @return this for chaining
87
*/
88
HttpServer exceptionHandler(Handler<Throwable> handler);
89
90
/**
91
* Set invalid request handler
92
* @param handler Handler for invalid requests
93
* @return this for chaining
94
*/
95
HttpServer invalidRequestHandler(Handler<HttpServerRequest> handler);
96
}
97
```
98
99
### HTTP Client Creation
100
101
Create and configure HTTP clients for making outbound requests.
102
103
```java { .api }
104
/**
105
* Create an HTTP client with default options
106
* @return HttpClient instance
107
*/
108
HttpClient createHttpClient();
109
110
/**
111
* Create an HTTP client with custom options
112
* @param options Client configuration options
113
* @return HttpClient instance
114
*/
115
HttpClient createHttpClient(HttpClientOptions options);
116
117
/**
118
* Create an HTTP client builder for advanced configuration
119
* @return HttpClientBuilder for fluent configuration
120
*/
121
HttpClientBuilder httpClientBuilder();
122
123
/**
124
* HTTP Client interface for making requests
125
*/
126
interface HttpClient extends Measured, Closeable {
127
/**
128
* Create a request
129
* @param method HTTP method
130
* @param port Target port
131
* @param host Target host
132
* @param requestURI Request URI
133
* @return Future that completes with HttpClientRequest
134
*/
135
Future<HttpClientRequest> request(HttpMethod method, int port, String host, String requestURI);
136
137
/**
138
* Create a request with options
139
* @param options Request options
140
* @return Future that completes with HttpClientRequest
141
*/
142
Future<HttpClientRequest> request(RequestOptions options);
143
144
/**
145
* Make a GET request
146
* @param port Target port
147
* @param host Target host
148
* @param requestURI Request URI
149
* @return Future that completes with HttpClientResponse
150
*/
151
Future<HttpClientResponse> get(int port, String host, String requestURI);
152
153
/**
154
* Make a POST request
155
* @param port Target port
156
* @param host Target host
157
* @param requestURI Request URI
158
* @return Future that completes with HttpClientResponse
159
*/
160
Future<HttpClientResponse> post(int port, String host, String requestURI);
161
162
/**
163
* Make a PUT request
164
* @param port Target port
165
* @param host Target host
166
* @param requestURI Request URI
167
* @return Future that completes with HttpClientResponse
168
*/
169
Future<HttpClientResponse> put(int port, String host, String requestURI);
170
171
/**
172
* Make a DELETE request
173
* @param port Target port
174
* @param host Target host
175
* @param requestURI Request URI
176
* @return Future that completes with HttpClientResponse
177
*/
178
Future<HttpClientResponse> delete(int port, String host, String requestURI);
179
180
/**
181
* Make a HEAD request
182
* @param port Target port
183
* @param host Target host
184
* @param requestURI Request URI
185
* @return Future that completes with HttpClientResponse
186
*/
187
Future<HttpClientResponse> head(int port, String host, String requestURI);
188
189
/**
190
* Create a WebSocket connection
191
* @param port Target port
192
* @param host Target host
193
* @param requestURI Request URI
194
* @return Future that completes with WebSocket
195
*/
196
Future<WebSocket> webSocket(int port, String host, String requestURI);
197
198
/**
199
* Close the client
200
* @return Future that completes when client is closed
201
*/
202
Future<Void> close();
203
204
/**
205
* Set connection handler
206
* @param handler Handler for new connections
207
* @return this for chaining
208
*/
209
HttpClient connectionHandler(Handler<HttpConnection> handler);
210
211
/**
212
* Set redirect handler
213
* @param handler Handler for redirects
214
* @return this for chaining
215
*/
216
HttpClient redirectHandler(Function<HttpClientResponse, Future<RequestOptions>> handler);
217
}
218
```
219
220
### HTTP Server Request Handling
221
222
Handle incoming HTTP requests with full access to headers, parameters, and body.
223
224
```java { .api }
225
/**
226
* Incoming HTTP request to server
227
*/
228
interface HttpServerRequest extends ReadStream<Buffer> {
229
/**
230
* Get the HTTP method
231
* @return HTTP method
232
*/
233
HttpMethod method();
234
235
/**
236
* Get the request URI
237
* @return Request URI
238
*/
239
String uri();
240
241
/**
242
* Get the request path
243
* @return Request path
244
*/
245
String path();
246
247
/**
248
* Get the query string
249
* @return Query string
250
*/
251
String query();
252
253
/**
254
* Get query parameters as MultiMap
255
* @return Query parameters
256
*/
257
MultiMap params();
258
259
/**
260
* Get headers as MultiMap
261
* @return Request headers
262
*/
263
MultiMap headers();
264
265
/**
266
* Get form attributes (for form-encoded requests)
267
* @return Future that completes with form attributes
268
*/
269
Future<MultiMap> formAttributes();
270
271
/**
272
* Get the request body as Buffer
273
* @return Future that completes with body buffer
274
*/
275
Future<Buffer> body();
276
277
/**
278
* Get the request body as JsonObject
279
* @return Future that completes with JSON object
280
*/
281
Future<JsonObject> bodyAsJson();
282
283
/**
284
* Get the request body as JsonArray
285
* @return Future that completes with JSON array
286
*/
287
Future<JsonArray> bodyAsJsonArray();
288
289
/**
290
* Get the request body as String
291
* @return Future that completes with body string
292
*/
293
Future<String> bodyAsString();
294
295
/**
296
* Get the HTTP response for this request
297
* @return HttpServerResponse instance
298
*/
299
HttpServerResponse response();
300
301
/**
302
* Get remote address
303
* @return Remote socket address
304
*/
305
SocketAddress remoteAddress();
306
307
/**
308
* Get local address
309
* @return Local socket address
310
*/
311
SocketAddress localAddress();
312
313
/**
314
* Get HTTP version
315
* @return HTTP version
316
*/
317
HttpVersion version();
318
319
/**
320
* Check if connection is SSL
321
* @return true if SSL
322
*/
323
boolean isSSL();
324
325
/**
326
* Get the scheme (http/https)
327
* @return Scheme string
328
*/
329
String scheme();
330
331
/**
332
* Get the host header
333
* @return Host string
334
*/
335
String host();
336
337
/**
338
* Get cookies as map
339
* @return Cookie map
340
*/
341
Map<String, Cookie> cookieMap();
342
343
/**
344
* Set upload handler for file uploads
345
* @param handler Upload handler
346
* @return this for chaining
347
*/
348
HttpServerRequest uploadHandler(Handler<HttpServerFileUpload> handler);
349
350
/**
351
* Upgrade to WebSocket
352
* @return Future that completes with ServerWebSocket
353
*/
354
Future<ServerWebSocket> toWebSocket();
355
356
/**
357
* Check if WebSocket upgrade is expected
358
* @return true if WebSocket upgrade
359
*/
360
boolean isExpectMultipart();
361
362
/**
363
* Set expect multipart
364
* @param expect Whether to expect multipart
365
* @return this for chaining
366
*/
367
HttpServerRequest setExpectMultipart(boolean expect);
368
}
369
```
370
371
### HTTP Server Response
372
373
Send HTTP responses with headers, status codes, and body content.
374
375
```java { .api }
376
/**
377
* Outgoing HTTP response from server
378
*/
379
interface HttpServerResponse extends WriteStream<Buffer> {
380
/**
381
* Set the status code
382
* @param statusCode HTTP status code
383
* @return this for chaining
384
*/
385
HttpServerResponse setStatusCode(int statusCode);
386
387
/**
388
* Get the status code
389
* @return HTTP status code
390
*/
391
int getStatusCode();
392
393
/**
394
* Set the status message
395
* @param statusMessage HTTP status message
396
* @return this for chaining
397
*/
398
HttpServerResponse setStatusMessage(String statusMessage);
399
400
/**
401
* Get the status message
402
* @return HTTP status message
403
*/
404
String getStatusMessage();
405
406
/**
407
* Get response headers
408
* @return Response headers MultiMap
409
*/
410
MultiMap headers();
411
412
/**
413
* Get response trailers
414
* @return Response trailers MultiMap
415
*/
416
MultiMap trailers();
417
418
/**
419
* Write data to response
420
* @param chunk Data to write
421
* @return Future that completes when written
422
*/
423
Future<Void> write(Buffer chunk);
424
425
/**
426
* Write string to response
427
* @param chunk String to write
428
* @return Future that completes when written
429
*/
430
Future<Void> write(String chunk);
431
432
/**
433
* Write string with encoding to response
434
* @param chunk String to write
435
* @param enc Character encoding
436
* @return Future that completes when written
437
*/
438
Future<Void> write(String chunk, String enc);
439
440
/**
441
* End the response
442
* @return Future that completes when ended
443
*/
444
Future<Void> end();
445
446
/**
447
* End the response with data
448
* @param chunk Final data to write
449
* @return Future that completes when ended
450
*/
451
Future<Void> end(Buffer chunk);
452
453
/**
454
* End the response with string
455
* @param chunk Final string to write
456
* @return Future that completes when ended
457
*/
458
Future<Void> end(String chunk);
459
460
/**
461
* Send a file
462
* @param filename File to send
463
* @return Future that completes when file is sent
464
*/
465
Future<Void> sendFile(String filename);
466
467
/**
468
* Send a file with offset and length
469
* @param filename File to send
470
* @param offset Offset in file
471
* @param length Length to send
472
* @return Future that completes when file is sent
473
*/
474
Future<Void> sendFile(String filename, long offset, long length);
475
476
/**
477
* Close the response
478
* @return Future that completes when closed
479
*/
480
Future<Void> close();
481
482
/**
483
* Check if response is closed
484
* @return true if closed
485
*/
486
boolean closed();
487
488
/**
489
* Check if headers have been written
490
* @return true if headers written
491
*/
492
boolean headWritten();
493
494
/**
495
* Set body end handler
496
* @param handler Handler called when body ends
497
* @return this for chaining
498
*/
499
HttpServerResponse bodyEndHandler(Handler<Void> handler);
500
501
/**
502
* Add a cookie
503
* @param cookie Cookie to add
504
* @return this for chaining
505
*/
506
HttpServerResponse addCookie(Cookie cookie);
507
508
/**
509
* Remove a cookie
510
* @param name Cookie name to remove
511
* @return Cookie that was removed
512
*/
513
Cookie removeCookie(String name);
514
515
/**
516
* Set chunked transfer encoding
517
* @param chunked Whether to use chunked encoding
518
* @return this for chaining
519
*/
520
HttpServerResponse setChunked(boolean chunked);
521
522
/**
523
* Check if chunked
524
* @return true if chunked
525
*/
526
boolean isChunked();
527
}
528
```
529
530
### HTTP Client Request
531
532
Create and send HTTP requests with full control over headers, body, and options.
533
534
```java { .api }
535
/**
536
* Outgoing HTTP request from client
537
*/
538
interface HttpClientRequest extends WriteStream<Buffer> {
539
/**
540
* Get the HTTP method
541
* @return HTTP method
542
*/
543
HttpMethod method();
544
545
/**
546
* Get the URI
547
* @return Request URI
548
*/
549
String uri();
550
551
/**
552
* Get request headers
553
* @return Request headers MultiMap
554
*/
555
MultiMap headers();
556
557
/**
558
* Get authority (host:port)
559
* @return Authority string
560
*/
561
String authority();
562
563
/**
564
* Write data to request
565
* @param chunk Data to write
566
* @return Future that completes when written
567
*/
568
Future<Void> write(Buffer chunk);
569
570
/**
571
* Write string to request
572
* @param chunk String to write
573
* @return Future that completes when written
574
*/
575
Future<Void> write(String chunk);
576
577
/**
578
* End the request
579
* @return Future that completes when ended
580
*/
581
Future<Void> end();
582
583
/**
584
* End the request with data
585
* @param chunk Final data to write
586
* @return Future that completes when ended
587
*/
588
Future<Void> end(Buffer chunk);
589
590
/**
591
* End the request with string
592
* @param chunk Final string to write
593
* @return Future that completes when ended
594
*/
595
Future<Void> end(String chunk);
596
597
/**
598
* Send the request and get response
599
* @return Future that completes with response
600
*/
601
Future<HttpClientResponse> send();
602
603
/**
604
* Send the request with body
605
* @param body Request body
606
* @return Future that completes with response
607
*/
608
Future<HttpClientResponse> send(Buffer body);
609
610
/**
611
* Send the request with string body
612
* @param body Request body
613
* @return Future that completes with response
614
*/
615
Future<HttpClientResponse> send(String body);
616
617
/**
618
* Get the response future
619
* @return Future that completes with response
620
*/
621
Future<HttpClientResponse> response();
622
623
/**
624
* Get the underlying connection
625
* @return HTTP connection
626
*/
627
HttpConnection connection();
628
629
/**
630
* Set request timeout
631
* @param timeoutMs Timeout in milliseconds
632
* @return this for chaining
633
*/
634
HttpClientRequest setTimeout(long timeoutMs);
635
636
/**
637
* Set push handler for HTTP/2 server push
638
* @param handler Push handler
639
* @return this for chaining
640
*/
641
HttpClientRequest pushHandler(Handler<HttpClientRequest> handler);
642
643
/**
644
* Reset the request (HTTP/2)
645
* @param code Reset code
646
* @return Future that completes when reset
647
*/
648
Future<Void> reset(long code);
649
650
/**
651
* Set stream priority (HTTP/2)
652
* @param streamPriority Stream priority
653
* @return this for chaining
654
*/
655
HttpClientRequest setStreamPriority(StreamPriority streamPriority);
656
657
/**
658
* Get stream priority (HTTP/2)
659
* @return Stream priority
660
*/
661
StreamPriority getStreamPriority();
662
}
663
```
664
665
### HTTP Client Response
666
667
Handle HTTP responses with access to status, headers, and body content.
668
669
```java { .api }
670
/**
671
* Incoming HTTP response to client
672
*/
673
interface HttpClientResponse extends ReadStream<Buffer> {
674
/**
675
* Get the status code
676
* @return HTTP status code
677
*/
678
int statusCode();
679
680
/**
681
* Get the status message
682
* @return HTTP status message
683
*/
684
String statusMessage();
685
686
/**
687
* Get response headers
688
* @return Response headers MultiMap
689
*/
690
MultiMap headers();
691
692
/**
693
* Get response trailers
694
* @return Response trailers MultiMap
695
*/
696
MultiMap trailers();
697
698
/**
699
* Get cookies
700
* @return List of cookies
701
*/
702
List<String> cookies();
703
704
/**
705
* Get HTTP version
706
* @return HTTP version
707
*/
708
HttpVersion version();
709
710
/**
711
* Get the response body as Buffer
712
* @return Future that completes with body buffer
713
*/
714
Future<Buffer> body();
715
716
/**
717
* Get the response body as JsonObject
718
* @return Future that completes with JSON object
719
*/
720
Future<JsonObject> bodyAsJson();
721
722
/**
723
* Get the response body as JsonArray
724
* @return Future that completes with JSON array
725
*/
726
Future<JsonArray> bodyAsJsonArray();
727
728
/**
729
* Get the response body as String
730
* @return Future that completes with body string
731
*/
732
Future<String> bodyAsString();
733
734
/**
735
* Get the request that produced this response
736
* @return Original HttpClientRequest
737
*/
738
HttpClientRequest request();
739
740
/**
741
* Get the underlying net socket
742
* @return NetSocket if available
743
*/
744
NetSocket netSocket();
745
746
/**
747
* Set custom frame handler (HTTP/2)
748
* @param handler Custom frame handler
749
* @return this for chaining
750
*/
751
HttpClientResponse customFrameHandler(Handler<HttpFrame> handler);
752
}
753
```
754
755
### WebSocket Support
756
757
Full WebSocket client and server support with frame handling.
758
759
```java { .api }
760
/**
761
* Base WebSocket interface
762
*/
763
interface WebSocket extends ReadStream<Buffer>, WriteStream<Buffer> {
764
/**
765
* Write binary message
766
* @param data Binary data
767
* @return Future that completes when written
768
*/
769
Future<Void> writeBinaryMessage(Buffer data);
770
771
/**
772
* Write text message
773
* @param text Text message
774
* @return Future that completes when written
775
*/
776
Future<Void> writeTextMessage(String text);
777
778
/**
779
* Write ping frame
780
* @param data Ping data
781
* @return Future that completes when written
782
*/
783
Future<Void> writePing(Buffer data);
784
785
/**
786
* Write pong frame
787
* @param data Pong data
788
* @return Future that completes when written
789
*/
790
Future<Void> writePong(Buffer data);
791
792
/**
793
* Set frame handler
794
* @param handler Frame handler
795
* @return this for chaining
796
*/
797
WebSocket frameHandler(Handler<WebSocketFrame> handler);
798
799
/**
800
* Set text message handler
801
* @param handler Text message handler
802
* @return this for chaining
803
*/
804
WebSocket textMessageHandler(Handler<String> handler);
805
806
/**
807
* Set binary message handler
808
* @param handler Binary message handler
809
* @return this for chaining
810
*/
811
WebSocket binaryMessageHandler(Handler<Buffer> handler);
812
813
/**
814
* Set pong handler
815
* @param handler Pong handler
816
* @return this for chaining
817
*/
818
WebSocket pongHandler(Handler<Buffer> handler);
819
820
/**
821
* Set close handler
822
* @param handler Close handler
823
* @return this for chaining
824
*/
825
WebSocket closeHandler(Handler<Void> handler);
826
827
/**
828
* Close the WebSocket
829
* @return Future that completes when closed
830
*/
831
Future<Void> close();
832
833
/**
834
* Close with code and reason
835
* @param statusCode Close status code
836
* @param reason Close reason
837
* @return Future that completes when closed
838
*/
839
Future<Void> close(short statusCode, String reason);
840
841
/**
842
* Check if SSL
843
* @return true if SSL
844
*/
845
boolean isSsl();
846
847
/**
848
* Get remote address
849
* @return Remote socket address
850
*/
851
SocketAddress remoteAddress();
852
853
/**
854
* Get local address
855
* @return Local socket address
856
*/
857
SocketAddress localAddress();
858
859
/**
860
* Get headers
861
* @return Headers MultiMap
862
*/
863
MultiMap headers();
864
}
865
866
/**
867
* Server-side WebSocket
868
*/
869
interface ServerWebSocket extends WebSocket {
870
/**
871
* Get the URI
872
* @return WebSocket URI
873
*/
874
String uri();
875
876
/**
877
* Get the path
878
* @return WebSocket path
879
*/
880
String path();
881
882
/**
883
* Get the query
884
* @return Query string
885
*/
886
String query();
887
888
/**
889
* Reject the WebSocket upgrade
890
* @param status Rejection status code
891
* @return Future that completes when rejected
892
*/
893
Future<Void> reject(int status);
894
895
/**
896
* Accept the WebSocket upgrade
897
* @return Future that completes when accepted
898
*/
899
Future<Void> accept();
900
901
/**
902
* Set handshake
903
* @param future Future to complete handshake
904
* @return this for chaining
905
*/
906
ServerWebSocket setHandshake(Future<Integer> future);
907
}
908
```
909
910
### Configuration Options
911
912
Comprehensive configuration options for HTTP servers and clients.
913
914
```java { .api }
915
/**
916
* HTTP Server configuration options
917
*/
918
class HttpServerOptions extends NetServerOptions {
919
HttpServerOptions setCompressionSupported(boolean compressionSupported);
920
HttpServerOptions setCompressionLevel(int compressionLevel);
921
HttpServerOptions setMaxWebSocketFrameSize(int maxWebSocketFrameSize);
922
HttpServerOptions setMaxWebSocketMessageSize(int maxWebSocketMessageSize);
923
HttpServerOptions setWebSocketSubProtocols(List<String> webSocketSubProtocols);
924
HttpServerOptions setHandle100ContinueAutomatically(boolean handle100ContinueAutomatically);
925
HttpServerOptions setMaxChunkSize(int maxChunkSize);
926
HttpServerOptions setMaxInitialLineLength(int maxInitialLineLength);
927
HttpServerOptions setMaxHeaderSize(int maxHeaderSize);
928
HttpServerOptions setMaxFormAttributeSize(int maxFormAttributeSize);
929
HttpServerOptions setDecompressionSupported(boolean decompressionSupported);
930
HttpServerOptions setAcceptUnmaskedFrames(boolean acceptUnmaskedFrames);
931
HttpServerOptions setInitialSettings(Http2Settings initialSettings);
932
HttpServerOptions setHttp2ConnectionWindowSize(int http2ConnectionWindowSize);
933
}
934
935
/**
936
* HTTP Client configuration options
937
*/
938
class HttpClientOptions extends NetClientOptions {
939
HttpClientOptions setKeepAlive(boolean keepAlive);
940
HttpClientOptions setMaxPoolSize(int maxPoolSize);
941
HttpClientOptions setMaxWaitQueueSize(int maxWaitQueueSize);
942
HttpClientOptions setHttp2MaxPoolSize(int http2MaxPoolSize);
943
HttpClientOptions setHttp2MultiplexingLimit(int http2MultiplexingLimit);
944
HttpClientOptions setHttp2ConnectionWindowSize(int http2ConnectionWindowSize);
945
HttpClientOptions setPipelining(boolean pipelining);
946
HttpClientOptions setPipeliningLimit(int pipeliningLimit);
947
HttpClientOptions setTryUseCompression(boolean tryUseCompression);
948
HttpClientOptions setMaxWebSocketFrameSize(int maxWebSocketFrameSize);
949
HttpClientOptions setMaxWebSocketMessageSize(int maxWebSocketMessageSize);
950
HttpClientOptions setDefaultHost(String defaultHost);
951
HttpClientOptions setDefaultPort(int defaultPort);
952
HttpClientOptions setMaxRedirects(int maxRedirects);
953
HttpClientOptions setForceSni(boolean forceSni);
954
HttpClientOptions setDecoderInitialBufferSize(int decoderInitialBufferSize);
955
HttpClientOptions setPoolCleanerPeriod(int poolCleanerPeriod);
956
HttpClientOptions setKeepAliveTimeout(int keepAliveTimeout);
957
}
958
959
/**
960
* HTTP methods enumeration
961
*/
962
enum HttpMethod {
963
OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, PATCH, OTHER
964
}
965
966
/**
967
* HTTP versions enumeration
968
*/
969
enum HttpVersion {
970
HTTP_1_0, HTTP_1_1, HTTP_2
971
}
972
973
/**
974
* WebSocket frame types
975
*/
976
enum WebSocketFrameType {
977
BINARY, TEXT, CLOSE, PING, PONG, CONTINUATION
978
}
979
980
/**
981
* Cookie interface
982
*/
983
interface Cookie {
984
String getName();
985
String getValue();
986
Cookie setDomain(String domain);
987
String getDomain();
988
Cookie setPath(String path);
989
String getPath();
990
Cookie setMaxAge(long maxAge);
991
Cookie setSecure(boolean secure);
992
boolean isSecure();
993
Cookie setHttpOnly(boolean httpOnly);
994
boolean isHttpOnly();
995
Cookie setSameSite(CookieSameSite sameSite);
996
CookieSameSite getSameSite();
997
String encode();
998
Cookie setPartitioned(boolean partitioned);
999
boolean isPartitioned();
1000
}
1001
```
1002
1003
## Usage Examples
1004
1005
**Basic HTTP Server:**
1006
1007
```java
1008
import io.vertx.core.Vertx;
1009
import io.vertx.core.http.HttpServer;
1010
1011
Vertx vertx = Vertx.vertx();
1012
1013
HttpServer server = vertx.createHttpServer();
1014
1015
server.requestHandler(request -> {
1016
String path = request.path();
1017
HttpMethod method = request.method();
1018
1019
// Handle different paths
1020
if (path.equals("/api/users") && method == HttpMethod.GET) {
1021
request.response()
1022
.putHeader("content-type", "application/json")
1023
.end("{\"users\": []}");
1024
} else if (path.equals("/api/users") && method == HttpMethod.POST) {
1025
request.bodyAsJson().onSuccess(json -> {
1026
// Process JSON body
1027
request.response()
1028
.setStatusCode(201)
1029
.putHeader("content-type", "application/json")
1030
.end("{\"id\": 123}");
1031
});
1032
} else {
1033
request.response().setStatusCode(404).end("Not Found");
1034
}
1035
});
1036
1037
server.listen(8080).onSuccess(server -> {
1038
System.out.println("HTTP server started on port 8080");
1039
});
1040
```
1041
1042
**HTTP Client Requests:**
1043
1044
```java
1045
import io.vertx.core.Vertx;
1046
import io.vertx.core.http.HttpClient;
1047
import io.vertx.core.http.HttpMethod;
1048
1049
Vertx vertx = Vertx.vertx();
1050
HttpClient client = vertx.createHttpClient();
1051
1052
// GET request
1053
client.get(80, "example.com", "/api/data")
1054
.onSuccess(response -> {
1055
System.out.println("Status: " + response.statusCode());
1056
response.bodyAsString().onSuccess(body -> {
1057
System.out.println("Body: " + body);
1058
});
1059
});
1060
1061
// POST request with JSON body
1062
client.request(HttpMethod.POST, 80, "example.com", "/api/users")
1063
.onSuccess(request -> {
1064
request.putHeader("content-type", "application/json");
1065
1066
JsonObject user = new JsonObject()
1067
.put("name", "John Doe")
1068
.put("email", "john@example.com");
1069
1070
request.send(user.encode()).onSuccess(response -> {
1071
System.out.println("User created: " + response.statusCode());
1072
});
1073
});
1074
```
1075
1076
**WebSocket Server:**
1077
1078
```java
1079
import io.vertx.core.http.ServerWebSocket;
1080
1081
HttpServer server = vertx.createHttpServer();
1082
1083
server.webSocketHandler(webSocket -> {
1084
System.out.println("WebSocket connected: " + webSocket.path());
1085
1086
webSocket.textMessageHandler(message -> {
1087
System.out.println("Received: " + message);
1088
webSocket.writeTextMessage("Echo: " + message);
1089
});
1090
1091
webSocket.closeHandler(v -> {
1092
System.out.println("WebSocket disconnected");
1093
});
1094
});
1095
1096
server.listen(8080);
1097
```
1098
1099
**File Upload Handling:**
1100
1101
```java
1102
server.requestHandler(request -> {
1103
if (request.path().equals("/upload") && request.method() == HttpMethod.POST) {
1104
request.setExpectMultipart(true);
1105
1106
request.uploadHandler(upload -> {
1107
String filename = upload.filename();
1108
System.out.println("Uploading file: " + filename);
1109
1110
upload.streamToFileSystem("uploads/" + filename)
1111
.onSuccess(v -> {
1112
request.response()
1113
.setStatusCode(200)
1114
.end("File uploaded successfully");
1115
})
1116
.onFailure(err -> {
1117
request.response()
1118
.setStatusCode(500)
1119
.end("Upload failed: " + err.getMessage());
1120
});
1121
});
1122
}
1123
});
1124
```