-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.html
More file actions
4906 lines (3830 loc) · 250 KB
/
Copy pathhttp.html
File metadata and controls
4906 lines (3830 loc) · 250 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html><html><body><title>HTTP Tutorial</title>
<h1>HTTP Tutorial</h1>
<p>The Hypertext Transfer Protocol (HTTP) is an application-level protocol for distributed, collaborative, hypermedia information systems. This is the foundation for data communication for the World Wide Web (i.e. internet) since 1990. HTTP is a generic and stateless protocol which can be used for other purposes as well using extensions of its request methods, error codes, and headers.</p>
<p>This tutorial is based on RFC-2616 specification, which defines the protocol referred to as HTTP/1.1. HTTP/1.1 is a revision of the original HTTP (HTTP/1.0). A major difference between HTTP/1.0 and HTTP/1.1 is that HTTP/1.0 uses a new connection for each request/response exchange, where as HTTP/1.1 connection may be used for one or more request/response exchanges.</p>
<h1>Audience</h1>
<p>This tutorial has been prepared for computer science graduates and web developers to help them understand the basic to advanced level concepts related to Hypertext Transfer Protocol (HTTP).</p>
<h1>Prerequisites</h1>
<p>Before proceeding with this tutorial, it is good to have a basic understanding of web concepts, web browsers, web servers, client and server architecture based software.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>HTTP Overview</title>
<h1>HTTP - Overview</h1>
<p>The Hypertext Transfer Protocol (HTTP) is an application-level protocol for distributed, collaborative, hypermedia information systems. This is the foundation for data communication for the World Wide Web (i.e. internet) since 1990. HTTP is a generic and stateless protocol which can be used for other purposes as well using extensions of its request methods, error codes, and headers.</p>
<p>Basically, HTTP is a TCP/IP based communication protocol, that is used to deliver data (HTML files, image files, query results, etc.) on the World Wide Web. The default port is TCP 80, but other ports can be used as well. It provides a standardized way for computers to communicate with each other. HTTP specification specifies how clients' request data will be constructed and sent to the server, and how the servers respond to these requests.</p>
<h2>Basic Features</h2>
<p>There are three basic features that make HTTP a simple but powerful protocol:</p>
<p><b>HTTP is connectionless:</b> The HTTP client, i.e., a browser initiates an HTTP request and after a request is made, the client disconnects from the server and waits for a response. The server processes the request and re-establishes the connection with the client to send a response back.</p>
<p><b>HTTP is media independent:</b> It means, any type of data can be sent by HTTP as long as both the client and the server know how to handle the data content. It is required for the client as well as the server to specify the content type using appropriate MIME-type.</p>
<p><b>HTTP is stateless:</b> As mentioned above, HTTP is connectionless and it is a direct result of HTTP being a stateless protocol. The server and client are aware of each other only during a current request. Afterwards, both of them forget about each other. Due to this nature of the protocol, neither the client nor the browser can retain information between different requests across the web pages.</p>
<h2>Basic Architecture</h2>
<p>The following diagram shows a very basic architecture of a web application and depicts where HTTP sits:</p>
<p>The HTTP protocol is a request/response protocol based on the client/server based architecture where web browsers, robots and search engines, etc. act like HTTP clients, and the Web server acts as a server.</p>
<h3>Client</h3>
<p>The HTTP client sends a request to the server in the form of a request method, URI, and protocol version, followed by a MIME-like message containing request modifiers, client information, and possible body content over a TCP/IP connection.</p>
<h3>Server</h3>
<p>The HTTP server responds with a status line, including the message's protocol version and a success or error code, followed by a MIME-like message containing server information, entity meta information, and possible entity-body content.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>HTTP Parameters</title>
<h1>HTTP - Parameters</h1>
<p>This chapter is going to list down few of the important HTTP Protocol Parameters and their syntax the way they are used in the communication. For example, format for date, format of URL, etc. This will help you in constructing your request and response messages while writing HTTP client or server programs. You will see the complete usage of these parameters in subsequent chapters while learning the message structure for HTTP requests and responses.</p>
<h2>HTTP Version</h2>
<p>HTTP uses a <b><major>.<minor></b> numbering scheme to indicate versions of the protocol. The version of an HTTP message is indicated by an HTTP-Version field in the first line. Here is the general syntax of specifying HTTP version number:</p>
<h3>Example</h3>
<h2>Uniform Resource Identifiers</h2>
<p>Uniform Resource Identifiers (URI) are simply formatted, case-insensitive string containing name, location, etc. to identify a resource, for example, a website, a web service, etc. A general syntax of URI used for HTTP is as follows:</p>
<p>Here if the <b>port</b> is empty or not given, port 80 is assumed for HTTP and an empty <b>abs_path</b> is equivalent to an <b>abs_path</b> of "/". The characters other than those in the <b>reserved</b> and <b>unsafe</b> sets are equivalent to their ""%" HEX HEX" encoding.</p>
<h3>Example</h3>
<p>The following three URIs are equivalent:</p>
<h2>Date/Time Formats</h2>
<p>All HTTP date/time stamps MUST be represented in Greenwich Mean Time (GMT), without exception. HTTP applications are allowed to use any of the following three representations of date/time stamps:</p>
<h2>Character Sets</h2>
<p>We use character sets to specify the character sets that the client prefers. Multiple character sets can be listed separated by commas. If a value is not specified, the default is the US-ASCII.</p>
<h3>Example</h3>
<p>Following are the valid character sets:</p>
<h2>Content Encodings</h2>
<p>A content encoding value indicates that an encoding algorithm has been used to encode the content before passing it over the network. Content coding are primarily used to allow a document to be compressed or otherwise usefully transformed without losing the identity.</p>
<p>All content-coding values are case-insensitive. HTTP/1.1 uses content-coding values in the Accept-Encoding and Content-Encoding header fields which we will see in the subsequent chapters.</p>
<h3>Example</h3>
<p>Following are the valid encoding schemes:</p>
<h2>Media Types</h2>
<p>HTTP uses Internet Media Types in the <b>Content-Type</b> and <b>Accept</b> header fields in order to provide open and extensible data typing and type negotiation. All the Media-type values are registered with the Internet Assigned Number Authority (IANA). The general syntax to specify media type is as follows:</p>
<p>The type, subtype, and parameter attribute names are case--insensitive.</p>
<h3>Example</h3>
<h2>Language Tags</h2>
<p>HTTP uses language tags within the <b>Accept-Language</b> and <b>Content-Language</b> fields. A language tag is composed of one or more parts: a primary language tag and a possibly empty series of subtags:</p>
<p>White spaces are not allowed within the tag and all tags are case- insensitive.</p>
<h3>Example</h3>
<p>Example tags include:</p>
<p>where any two-letter primary-tag is an ISO-639 language abbreviation and any two-letter initial subtag is an ISO-3166 country code.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate">
HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT
</pre>
<h3>Example</h3>
<pre class="prettyprint notranslate">
HTTP/1.0
or
HTTP/1.1
</pre>
<h2>Uniform Resource Identifiers</h2>
<p>Uniform Resource Identifiers (URI) are simply formatted, case-insensitive string containing name, location, etc. to identify a resource, for example, a website, a web service, etc. A general syntax of URI used for HTTP is as follows:</p>
<pre class="prettyprint notranslate">
URI = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]
</pre>
<p>Here if the <b>port</b> is empty or not given, port 80 is assumed for HTTP and an empty <b>abs_path</b> is equivalent to an <b>abs_path</b> of "/". The characters other than those in the <b>reserved</b> and <b>unsafe</b> sets are equivalent to their ""%" HEX HEX" encoding.</p>
<h3>Example</h3>
<p>The following three URIs are equivalent:</p>
<pre class="result notranslate">
http://abc.com:80/~smith/home.html
http://ABC.com/%7Esmith/home.html
http://ABC.com:/%7esmith/home.html
</pre>
<h2>Date/Time Formats</h2>
<p>All HTTP date/time stamps MUST be represented in Greenwich Mean Time (GMT), without exception. HTTP applications are allowed to use any of the following three representations of date/time stamps:</p>
<pre class="result notranslate">
Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
</pre>
<h2>Character Sets</h2>
<p>We use character sets to specify the character sets that the client prefers. Multiple character sets can be listed separated by commas. If a value is not specified, the default is the US-ASCII.</p>
<h3>Example</h3>
<p>Following are the valid character sets:</p>
<pre class="result notranslate">
US-ASCII
or
ISO-8859-1
or
ISO-8859-7
</pre>
<h2>Content Encodings</h2>
<p>A content encoding value indicates that an encoding algorithm has been used to encode the content before passing it over the network. Content coding are primarily used to allow a document to be compressed or otherwise usefully transformed without losing the identity.</p>
<p>All content-coding values are case-insensitive. HTTP/1.1 uses content-coding values in the Accept-Encoding and Content-Encoding header fields which we will see in the subsequent chapters.</p>
<h3>Example</h3>
<p>Following are the valid encoding schemes:</p>
<pre class="result notranslate">
Accept-encoding: gzip
or
Accept-encoding: compress
or
Accept-encoding: deflate
</pre>
<h2>Media Types</h2>
<p>HTTP uses Internet Media Types in the <b>Content-Type</b> and <b>Accept</b> header fields in order to provide open and extensible data typing and type negotiation. All the Media-type values are registered with the Internet Assigned Number Authority (IANA). The general syntax to specify media type is as follows:</p>
<pre class="result notranslate">
media-type = type "/" subtype *( ";" parameter )
</pre>
<p>The type, subtype, and parameter attribute names are case--insensitive.</p>
<h3>Example</h3>
<pre class="result notranslate">
Accept: image/gif
</pre>
<h2>Language Tags</h2>
<p>HTTP uses language tags within the <b>Accept-Language</b> and <b>Content-Language</b> fields. A language tag is composed of one or more parts: a primary language tag and a possibly empty series of subtags:</p>
<pre class="result notranslate">
language-tag = primary-tag *( "-" subtag )
</pre>
<p>White spaces are not allowed within the tag and all tags are case- insensitive.</p>
<h3>Example</h3>
<p>Example tags include:</p>
<pre class="result notranslate">
en, en-US, en-cockney, i-cherokee, x-pig-latin
</pre>
<title>HTTP Messages</title>
<h1>HTTP - Messages</h1>
<p>HTTP is based on the client-server architecture model and a stateless request/response protocol that operates by exchanging messages across a reliable TCP/IP connection.</p>
<p>An HTTP "client" is a program (Web browser or any other client) that establishes a connection to a server for the purpose of sending one or more HTTP request messages. An HTTP "server" is a program ( generally a web server like Apache Web Server or Internet Information Services IIS, etc. ) that accepts connections in order to serve HTTP requests by sending HTTP response messages.</p>
<p>HTTP requests and HTTP responses use a generic message format of RFC 822 for transferring the required data. This generic message format consists of the following four items.</p>
<p>In the following sections, we will explain each of the entities used in an HTTP message.</p>
<h2>Message Start-Line</h2>
<p>A start-line will have the following generic syntax:</p>
<p>We will discuss Request-Line and Status-Line while discussing HTTP Request and HTTP Response messages respectively. For now, let's see the examples of start line in case of request and response:</p>
<h2>Header Fields</h2>
<p>HTTP header fields provide required information about the request or response, or about the object sent in the message body. There are four types of HTTP message headers:</p>
<p><b>General-header:</b> These header fields have general applicability for both request and response messages.</p>
<p><b>Request-header:</b> These header fields have applicability only for request messages.</p>
<p><b>Response-header:</b> These header fields have applicability only for response messages.</p>
<p><b>Entity-header:</b> These header fields define meta information about the entity-body or, if no body is present, about the resource identified by the request.</p>
<p>All the above mentioned headers follow the same generic format and each of the header field consists of a name followed by a colon (<b>:</b>) and the field value as follows:</p>
<p>Following are the examples of various header fields:</p>
<h2>Message Body</h2>
<p>The message body part is optional for an HTTP message but if it is available, then it is used to carry the entity-body associated with the request or response. If entity body is associated, then usually <b>Content-Type</b> and <b>Content-Length</b> headers lines specify the nature of the body associated.</p>
<p>A message body is the one which carries the actual HTTP request data (including form data and uploaded, etc.) and HTTP response data from the server ( including files, images, etc.). Shown below is the simple content of a message body:</p>
<p>Next two chapters will make use of above explained concepts to prepare HTTP Requests and HTTP Responses.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate">
HTTP-message = <Request> | <Response> ; HTTP/1.1 messages
</pre>
<p>HTTP requests and HTTP responses use a generic message format of RFC 822 for transferring the required data. This generic message format consists of the following four items.</p>
<pre class="result notranslate">
<ul class="list">
<li>A Start-line</li>
<li>Zero or more header fields followed by CRLF</li>
<li>An empty line (i.e., a line with nothing preceding the CRLF)
indicating the end of the header fields</li>
<li>Optionally a message-body</li>
</ul>
</pre>
<p>In the following sections, we will explain each of the entities used in an HTTP message.</p>
<h2>Message Start-Line</h2>
<p>A start-line will have the following generic syntax:</p>
<pre class="result notranslate">
start-line = Request-Line | Status-Line
</pre>
<p>We will discuss Request-Line and Status-Line while discussing HTTP Request and HTTP Response messages respectively. For now, let's see the examples of start line in case of request and response:</p>
<pre class="result notranslate">
GET /hello.htm HTTP/1.1 (This is Request-Line sent by the client)
HTTP/1.1 200 OK (This is Status-Line sent by the server)
</pre>
<h2>Header Fields</h2>
<p>HTTP header fields provide required information about the request or response, or about the object sent in the message body. There are four types of HTTP message headers:</p>
<ul class="list">
<li><p><b>General-header:</b> These header fields have general applicability for both request and response messages.</p></li>
<li><p><b>Request-header:</b> These header fields have applicability only for request messages.</p></li>
<li><p><b>Response-header:</b> These header fields have applicability only for response messages.</p></li>
<li><p><b>Entity-header:</b> These header fields define meta information about the entity-body or, if no body is present, about the resource identified by the request.</p></li>
</ul>
<p>All the above mentioned headers follow the same generic format and each of the header field consists of a name followed by a colon (<b>:</b>) and the field value as follows:</p>
<pre class="result notranslate">
message-header = field-name ":" [ field-value ]
</pre>
<p>Following are the examples of various header fields:</p>
<pre class="result notranslate">
User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3
Host: www.example.com
Accept-Language: en, mi
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
ETag: "34aa387-d-1568eb00"
Accept-Ranges: bytes
Content-Length: 51
Vary: Accept-Encoding
Content-Type: text/plain
</pre>
<h2>Message Body</h2>
<p>The message body part is optional for an HTTP message but if it is available, then it is used to carry the entity-body associated with the request or response. If entity body is associated, then usually <b>Content-Type</b> and <b>Content-Length</b> headers lines specify the nature of the body associated.</p>
<p>A message body is the one which carries the actual HTTP request data (including form data and uploaded, etc.) and HTTP response data from the server ( including files, images, etc.). Shown below is the simple content of a message body:</p>
<pre class="prettyprint notranslate">
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>
</pre>
<title>HTTP Requests</title>
<h1>HTTP - Requests</h1>
<p>An HTTP client sends an HTTP request to a server in the form of a request message which includes following format:</p>
<p>The following sections explain each of the entities used in an HTTP request message.</p>
<h2>Request-Line</h2>
<p>The Request-Line begins with a method token, followed by the Request-URI and the protocol version, and ending with CRLF. The elements are separated by space SP characters.</p>
<p>Let's discuss each of the parts mentioned in the Request-Line.</p>
<h2>Request Method</h2>
<p>The request <b>method</b> indicates the method to be performed on the resource identified by the given <b>Request-URI</b>. The method is case-sensitive and should always be mentioned in uppercase. The following table lists all the supported methods in HTTP/1.1.</p>
<p>The GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.</p>
<p>Same as GET, but it transfers the status line and the header section only.</p>
<p>Replaces all the current representations of the target resource with the uploaded content.</p>
<p>Removes all the current representations of the target resource given by URI.</p>
<p>Establishes a tunnel to the server identified by a given URI.</p>
<p>Describe the communication options for the target resource.</p>
<p>Performs a message loop back test along with the path to the target resource.</p>
<h2>Request-URI</h2>
<p>The Request-URI is a Uniform Resource Identifier and identifies the resource upon which to apply the request. Following are the most commonly used forms to specify an URI:</p>
<p><b>OPTIONS * HTTP/1.1</b></p>
<p><b>GET http://www.w3.org/pub/WWW/TheProject.html HTTP/1.1</b></p>
<p><b>GET /pub/WWW/TheProject.html HTTP/1.1</b></p>
<p><b>Host: www.w3.org</b></p>
<p>Note that the absolute path cannot be empty; if none is present in the original URI, it MUST be given as "/" (the server root).</p>
<h2>Request Header Fields</h2>
<p>We will study General-header and Entity-header in a separate chapter when we will learn HTTP header fields. For now, let's check what Request header fields are.</p>
<p>The request-header fields allow the client to pass additional information about the request, and about the client itself, to the server. These fields act as request modifiers.Here is a list of some important Request-header fields that can be used based on the requirement:</p>
<p>Accept-Charset</p>
<p> Accept-Encoding</p>
<p> Accept-Language</p>
<p> Authorization</p>
<p> Expect</p>
<p> From</p>
<p> Host</p>
<p> If-Match</p>
<p> If-Modified-Since</p>
<p> If-None-Match</p>
<p> If-Range</p>
<p> If-Unmodified-Since</p>
<p> Max-Forwards</p>
<p> Proxy-Authorization</p>
<p> Range</p>
<p> Referer</p>
<p> TE</p>
<p> User-Agent</p>
<p>You can introduce your custom fields in case you are going to write your own custom Client and Web Server.</p>
<h2> Examples of Request Message</h2>
<p>Now let's put it all together to form an HTTP request to fetch <b>hello.htm</b> page from the web server running on tutorialspoint.com</p>
<p>Here we are not sending any request data to the server because we are fetching a plain HTML page from the server. Connection is a general-header, and the rest of the headers are request headers. The following example shows how to send form data to the server using request message body:</p>
<p>Here the given URL <i>/cgi-bin/process.cgi</i> will be used to process the passed data and accordingly, a response will be returned. Here <b>content-type</b> tells the server that the passed data is a simple web form data and <b>length</b> will be the actual length of the data put in the message body. The following example shows how you can pass plain XML to your web server:</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result notranslate">
<ul class="list">
<li>A Request-line</li>
<li>Zero or more header (General|Request|Entity) fields followed by CRLF</li>
<li>An empty line (i.e., a line with nothing preceding the CRLF)
indicating the end of the header fields</li>
<li>Optionally a message-body</li>
</pre>
<p>The following sections explain each of the entities used in an HTTP request message.</p>
<h2>Request-Line</h2>
<p>The Request-Line begins with a method token, followed by the Request-URI and the protocol version, and ending with CRLF. The elements are separated by space SP characters.</p>
<pre class="result notranslate">
Request-Line = Method SP Request-URI SP HTTP-Version CRLF
</pre>
<p>Let's discuss each of the parts mentioned in the Request-Line.</p>
<h2>Request Method</h2>
<p>The request <b>method</b> indicates the method to be performed on the resource identified by the given <b>Request-URI</b>. The method is case-sensitive and should always be mentioned in uppercase. The following table lists all the supported methods in HTTP/1.1.</p>
<table class="table table-bordered">
<tr>
<th>S.N.</th>
<th>Method and Description</th>
</tr>
<tr>
<td>1</td>
<td><b>GET</b><p>The GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.</p></td>
</tr>
<tr>
<td>2</td>
<td><b>HEAD</b><p>Same as GET, but it transfers the status line and the header section only.</p></td>
</tr>
<tr>
<td>3</td>
<td><b>POST</b><p>A POST request is used to send data to the server, for example, customer information, file upload, etc. using HTML forms.</td></tr>
<tr>
<td>4</td>
<td><b>PUT</b><p>Replaces all the current representations of the target resource with the uploaded content.</p></td>
</tr>
<tr>
<td>5</td>
<td><b>DELETE</b><p>Removes all the current representations of the target resource given by URI.</p></td>
</tr>
<tr>
<td>6</td>
<td><b>CONNECT</b><p>Establishes a tunnel to the server identified by a given URI.</p></td>
</tr>
<tr>
<td>7</td>
<td><b>OPTIONS</b><p>Describe the communication options for the target resource.</p></td>
</tr>
<tr>
<td>8</td>
<td><b>TRACE</b><p>Performs a message loop back test along with the path to the target resource.</p></td>
</tr>
</table>
<h2>Request-URI</h2>
<p>The Request-URI is a Uniform Resource Identifier and identifies the resource upon which to apply the request. Following are the most commonly used forms to specify an URI:</p>
<pre class="result notranslate">
Request-URI = "*" | absoluteURI | abs_path | authority
</pre><div style="clear:both";> </div>
<table class="table table-bordered">
<tr>
<th>S.N.</th>
<th>Method and Description</th>
</tr>
<tr>
<td>1</td>
<td>The asterisk <b>*</b> is used when an HTTP request does not apply to a particular resource, but to the server itself, and is only allowed when the method used does not necessarily apply to a resource. For example:
<p><b>OPTIONS * HTTP/1.1</b></p></td>
</tr>
<tr>
<td>2</td>
<td>The <b>absoluteURI</b> is used when an HTTP request is being made to a proxy. The proxy is requested to forward the request or service from a valid cache, and return the response. For example:
<p><b>GET http://www.w3.org/pub/WWW/TheProject.html HTTP/1.1</b></p></td>
</tr>
<tr>
<td>3</td>
<td>The most common form of Request-URI is that used to identify a resource on an origin server or gateway. For example, a client wishing to retrieve a resource directly from the origin server would create a TCP connection to port 80 of the host "www.w3.org" and send the following lines:
<p><b>GET /pub/WWW/TheProject.html HTTP/1.1</b></p>
<p><b>Host: www.w3.org</b></p>
<p>Note that the absolute path cannot be empty; if none is present in the original URI, it MUST be given as "/" (the server root).</p>
</td>
</tr>
</table>
<h2>Request Header Fields</h2>
<p>We will study General-header and Entity-header in a separate chapter when we will learn HTTP header fields. For now, let's check what Request header fields are.</p>
<p>The request-header fields allow the client to pass additional information about the request, and about the client itself, to the server. These fields act as request modifiers.Here is a list of some important Request-header fields that can be used based on the requirement:</p>
<ul class="list">
<li><p>Accept-Charset</p></li>
<li><p> Accept-Encoding</p></li>
<li><p> Accept-Language</p></li>
<li><p> Authorization</p></li>
<li><p> Expect</p></li>
<li><p> From</p></li>
<li><p> Host</p></li>
<li><p> If-Match</p></li>
<li><p> If-Modified-Since</p></li>
<li><p> If-None-Match</p></li>
<li><p> If-Range</p></li>
<li><p> If-Unmodified-Since</p></li>
<li><p> Max-Forwards</p></li>
<li><p> Proxy-Authorization</p></li>
<li><p> Range</p></li>
<li><p> Referer</p></li>
<li><p> TE</p></li>
<li><p> User-Agent</p></li>
</ul>
<p>You can introduce your custom fields in case you are going to write your own custom Client and Web Server.</p>
<h2> Examples of Request Message</h2>
<p>Now let's put it all together to form an HTTP request to fetch <b>hello.htm</b> page from the web server running on tutorialspoint.com</p>
<pre class="result notranslate">
GET /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
</pre>
<p>Here we are not sending any request data to the server because we are fetching a plain HTML page from the server. Connection is a general-header, and the rest of the headers are request headers. The following example shows how to send form data to the server using request message body:</p>
<pre class="result notranslate">
POST /cgi-bin/process.cgi HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Content-Type: application/x-www-form-urlencoded
Content-Length: <b>length</b>
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
licenseID=string&content=string&/paramsXML=string
</pre>
<p>Here the given URL <i>/cgi-bin/process.cgi</i> will be used to process the passed data and accordingly, a response will be returned. Here <b>content-type</b> tells the server that the passed data is a simple web form data and <b>length</b> will be the actual length of the data put in the message body. The following example shows how you can pass plain XML to your web server:</p>
<pre class="result notranslate">
POST /cgi-bin/process.cgi HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Content-Type: text/xml; charset=utf-8
Content-Length: <b>length</b>
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://clearforest.com/">string</string>
</pre>
<title>HTTP Responses</title>
<h1>HTTP - Responses</h1>
<p>After receiving and interpreting a request message, a server responds with an HTTP response message:</p>
<p>The following sections explain each of the entities used in an HTTP response message.</p>
<h2>Message Status-Line</h2>
<p>A Status-Line consists of the protocol version followed by a numeric status code and its associated textual phrase. The elements are separated by space SP characters.</p>
<h2>HTTP Version</h2>
<p>A server supporting HTTP version 1.1 will return the following version information:</p>
<h2>Status Code</h2>
<p>The Status-Code element is a 3-digit integer where first digit of the Status-Code defines the class of response and the last two digits do not have any categorization role. There are 5 values for the first digit:</p>
<p> It means the request was received and the process is continuing.</p>
<p> It means the action was successfully received, understood, and accepted.</p>
<p> It means further action must be taken in order to complete the request.</p>
<p> It means the request contains incorrect syntax or cannot be fulfilled.</p>
<p> It means the server failed to fulfill an apparently valid request.</p>
<p>HTTP status codes are extensible and HTTP applications are not required to understand the meaning of all registered status codes. A list of all the status codes has been given in a separate chapter for your reference.</p>
<h2>Response Header Fields</h2>
<p>We will study General-header and Entity-header in a separate chapter when we will learn HTTP header fields. For now, let's check what Response header fields are.</p>
<p>The response-header fields allow the server to pass additional information about the response which cannot be placed in the Status- Line. These header fields give information about the server and about further access to the resource identified by the Request-URI.</p>
<p>Accept-Ranges</p>
<p>Age</p>
<p>ETag</p>
<p>Location</p>
<p>Proxy-Authenticate</p>
<p>Retry-After</p>
<p>Server</p>
<p>Vary</p>
<p>WWW-Authenticate</p>
<p>You can introduce your custom fields in case you are going to write your own custom Web Client and Server.</p>
<h2>Examples of Response Message</h2>
<p>Now let's put it all together to form an HTTP response for a request to fetch the <b>hello.htm</b> page from the web server running on tutorialspoint.com</p>
<p>The following example shows an HTTP response message displaying error condition when the web server could not find the requested page:</p>
<p>Following is an example of HTTP response message showing error condition when the web server encountered a wrong HTTP version in the given HTTP request:</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result notranslate">
<ul class="list">
<li>A Status-line</li>
<li>Zero or more header (General|Response|Entity) fields followed by CRLF</li>
<li>An empty line (i.e., a line with nothing preceding the CRLF)
indicating the end of the header fields</li>
<li>Optionally a message-body</li>
</pre>
<p>The following sections explain each of the entities used in an HTTP response message.</p>
<h2>Message Status-Line</h2>
<p>A Status-Line consists of the protocol version followed by a numeric status code and its associated textual phrase. The elements are separated by space SP characters.</p>
<pre class="result notranslate">
Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
</pre>
<h2>HTTP Version</h2>
<p>A server supporting HTTP version 1.1 will return the following version information:</p>
<pre class="result notranslate">
HTTP-Version = HTTP/1.1
</pre>
<h2>Status Code</h2>
<p>The Status-Code element is a 3-digit integer where first digit of the Status-Code defines the class of response and the last two digits do not have any categorization role. There are 5 values for the first digit:</p>
<table class="table table-bordered">
<tr>
<th>S.N.</th>
<th>Code and Description</th>
</tr>
<tr>
<td>1</td>
<td><b>1xx: Informational</b><p> It means the request was received and the process is continuing.</p></td>
</tr>
<tr>
<td>2</td>
<td><b>2xx: Success</b><p> It means the action was successfully received, understood, and accepted.</p></td>
</tr>
<tr>
<td>3</td>
<td><b>3xx: Redirection</b><p> It means further action must be taken in order to complete the request.</p></td>
</tr>
<tr>
<td>4</td>
<td><b>4xx: Client Error</b><p> It means the request contains incorrect syntax or cannot be fulfilled.</p></td>
</tr>
<tr>
<td>5</td>
<td><b>5xx: Server Error</b><p> It means the server failed to fulfill an apparently valid request.</p></td>
</tr>
</table>
<p>HTTP status codes are extensible and HTTP applications are not required to understand the meaning of all registered status codes. A list of all the status codes has been given in a separate chapter for your reference.</p>
<h2>Response Header Fields</h2>
<p>We will study General-header and Entity-header in a separate chapter when we will learn HTTP header fields. For now, let's check what Response header fields are.</p>
<p>The response-header fields allow the server to pass additional information about the response which cannot be placed in the Status- Line. These header fields give information about the server and about further access to the resource identified by the Request-URI.</p>
<ul class="list">
<li><p>Accept-Ranges</p></li>
<li><p>Age</p></li>
<li><p>ETag</p></li>
<li><p>Location</p></li>
<li><p>Proxy-Authenticate</p></li>
<li><p>Retry-After</p></li>
<li><p>Server</p></li>
<li><p>Vary</p></li>
<li><p>WWW-Authenticate</p></li>
</ul>
<p>You can introduce your custom fields in case you are going to write your own custom Web Client and Server.</p>
<h2>Examples of Response Message</h2>
<p>Now let's put it all together to form an HTTP response for a request to fetch the <b>hello.htm</b> page from the web server running on tutorialspoint.com</p>
<pre class="result notranslate">
HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
Content-Length: 88
Content-Type: text/html
Connection: Closed
</pre>
<pre class="prettyprint notranslate">
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>
</pre>
<p>The following example shows an HTTP response message displaying error condition when the web server could not find the requested page:</p>
<pre class="result notranslate">
HTTP/1.1 404 Not Found
Date: Sun, 18 Oct 2012 10:36:20 GMT
Server: Apache/2.2.14 (Win32)
Content-Length: 230
Connection: Closed
Content-Type: text/html; charset=iso-8859-1</pre>
<pre class="prettyprint notranslate"><!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>Not Found</h1>
<p>The requested URL /t.html was not found on this server.</p>
</body>
</html>
</pre>
<p>Following is an example of HTTP response message showing error condition when the web server encountered a wrong HTTP version in the given HTTP request:</p>
<pre class="result notranslate">
HTTP/1.1 400 Bad Request
Date: Sun, 18 Oct 2012 10:36:20 GMT
Server: Apache/2.2.14 (Win32)
Content-Length: 230
Content-Type: text/html; charset=iso-8859-1
Connection: Closed
</pre><pre class="prettyprint notranslate">
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head>
<title>400 Bad Request</title>
</head>
<body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.</p>
<p>The request line contained invalid characters following the protocol string.</p>
</body>
</html>
</pre>
<title>HTTP Methods</title>
<h1>HTTP - Methods</h1>
<p>The set of common methods for HTTP/1.1 is defined below and this set can be expanded based on requirements. These method names are case sensitive and they must be used in uppercase.</p>
<p>The GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.</p>
<p>Same as GET, but transfers the status line and header section only.</p>
<p>A POST request is used to send data to the server, for example, customer information, file upload, etc. using HTML forms.</p>
<p>Replaces all current representations of the target resource with the uploaded content.</p>
<p>Removes all current representations of the target resource given by a URI.</p>
<p>Establishes a tunnel to the server identified by a given URI.</p>
<p>Describes the communication options for the target resource.</p>
<p>Performs a message loop-back test along the path to the target resource.</p>
<h2>GET Method</h2>
<p>A GET request retrieves data from a web server by specifying parameters in the URL portion of the request. This is the main method used for document retrieval. The following example makes use of GET method to fetch hello.htm:</p>
<p>The server response against the above GET request will be as follows:</p>
<h2>HEAD Method</h2>
<p>The HEAD method is functionally similar to GET, except that the server replies with a response line and headers, but no entity-body. The following example makes use of HEAD method to fetch header information about hello.htm:</p>
<p>The server response against the above GET request will be as follows:</p>
<p>You can notice that here server the does not send any data after header.</p>
<h2>POST Method</h2>
<p>The POST method is used when you want to send some data to the server, for example, file update, form data, etc. The following example makes use of POST method to send a form data to the server, which will be processed by a process.cgi and finally a response will be returned:</p>
<p>The server side script process.cgi processes the passed data and sends the following response:</p>
<h2>PUT Method</h2>
<p>The PUT method is used to request the server to store the included entity-body at a location specified by the given URL. The following example requests the server to save the given entity-boy in <b>hello.htm</b> at the root of the server:</p>
<p>The server will store the given entity-body in <b>hello.htm</b> file and will send the following response back to the client:</p>
<h2>DELETE Method</h2>
<p>The DELETE method is used to request the server to delete a file at a location specified by the given URL. The following example requests the server to delete the given file <b>hello.htm</b> at the root of the server:</p>
<p>The server will delete the mentioned file <b>hello.htm</b> and will send the following response back to the client:</p>
<h2>CONNECT Method</h2>
<p>The CONNECT method is used by the client to establish a network connection to a web server over HTTP. The following example requests a connection with a web server running on the host tutorialspoint.com:</p>
<p>The connection is established with the server and the following response is sent back to the client:</p>
<h2>OPTIONS Method</h2>
<p>The OPTIONS method is used by the client to find out the HTTP methods and other options supported by a web server. The client can specify a URL for the OPTIONS method, or an asterisk (*) to refer to the entire server. The following example requests a list of methods supported by a web server running on tutorialspoint.com:</p>
<p>The server will send an information based on the current configuration of the server, for example:</p>
<h2>TRACE Method</h2>
<p>The TRACE method is used to echo the contents of an HTTP Request back to the requester which can be used for debugging purpose at the time of development. The following example shows the usage of TRACE method:</p>
<p>The server will send the following message in response to the above request:</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result notranslate">
GET /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
</pre>
<p>The server response against the above GET request will be as follows:</p>
<pre class="result notranslate">
HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
ETag: "34aa387-d-1568eb00"
Vary: Authorization,Accept
Accept-Ranges: bytes
Content-Length: 88
Content-Type: text/html
Connection: Closed
</pre>
<pre class="prettyprint notranslate">
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>
</pre>
<h2>HEAD Method</h2>
<p>The HEAD method is functionally similar to GET, except that the server replies with a response line and headers, but no entity-body. The following example makes use of HEAD method to fetch header information about hello.htm:</p>
<pre class="result notranslate">
HEAD /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
</pre>
<p>The server response against the above GET request will be as follows:</p>
<pre class="result notranslate">
HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
ETag: "34aa387-d-1568eb00"
Vary: Authorization,Accept
Accept-Ranges: bytes
Content-Length: 88
Content-Type: text/html
Connection: Closed
</pre>
<p>You can notice that here server the does not send any data after header.</p>
<h2>POST Method</h2>
<p>The POST method is used when you want to send some data to the server, for example, file update, form data, etc. The following example makes use of POST method to send a form data to the server, which will be processed by a process.cgi and finally a response will be returned:</p>
<pre class="result notranslate">
POST /cgi-bin/process.cgi HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Content-Type: text/xml; charset=utf-8
Content-Length: 88
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
</pre><pre class="prettyprint notranslate">
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://clearforest.com/">string</string>
</pre>
<p>The server side script process.cgi processes the passed data and sends the following response:</p>
<pre class="result notranslate">
HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
ETag: "34aa387-d-1568eb00"
Vary: Authorization,Accept
Accept-Ranges: bytes
Content-Length: 88
Content-Type: text/html
Connection: Closed
</pre><pre class="prettyprint notranslate">
<html>
<body>
<h1>Request Processed Successfully</h1>
</body>
</html>
</pre>
<h2>PUT Method</h2>
<p>The PUT method is used to request the server to store the included entity-body at a location specified by the given URL. The following example requests the server to save the given entity-boy in <b>hello.htm</b> at the root of the server:</p>
<pre class="result notranslate">
PUT /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Accept-Language: en-us
Connection: Keep-Alive
Content-type: text/html
Content-Length: 182
</pre><pre class="prettyprint notranslate">
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>
</pre>
<p>The server will store the given entity-body in <b>hello.htm</b> file and will send the following response back to the client:</p>
<pre class="result notranslate">
HTTP/1.1 201 Created
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Content-type: text/html
Content-length: 30
Connection: Closed
</pre><pre class="prettyprint notranslate">
<html>
<body>
<h1>The file was created.</h1>
</body>
</html>
</pre>
<h2>DELETE Method</h2>
<p>The DELETE method is used to request the server to delete a file at a location specified by the given URL. The following example requests the server to delete the given file <b>hello.htm</b> at the root of the server:</p>
<pre class="result notranslate">
DELETE /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Accept-Language: en-us
Connection: Keep-Alive
</pre>
<p>The server will delete the mentioned file <b>hello.htm</b> and will send the following response back to the client:</p>
<pre class="result notranslate">
HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Content-type: text/html
Content-length: 30
Connection: Closed
</pre><pre class="prettyprint notranslate">
<html>
<body>
<h1>URL deleted.</h1>
</body>
</html>
</pre>
<h2>CONNECT Method</h2>
<p>The CONNECT method is used by the client to establish a network connection to a web server over HTTP. The following example requests a connection with a web server running on the host tutorialspoint.com:</p>
<pre class="result notranslate">
CONNECT www.tutorialspoint.com HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
</pre>
<p>The connection is established with the server and the following response is sent back to the client:</p>
<pre class="result notranslate">
HTTP/1.1 200 Connection established
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
</pre>
<h2>OPTIONS Method</h2>
<p>The OPTIONS method is used by the client to find out the HTTP methods and other options supported by a web server. The client can specify a URL for the OPTIONS method, or an asterisk (*) to refer to the entire server. The following example requests a list of methods supported by a web server running on tutorialspoint.com:</p>
<pre class="result notranslate">
OPTIONS * HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
</pre>
<p>The server will send an information based on the current configuration of the server, for example:</p>
<pre class="result notranslate">
HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Allow: GET,HEAD,POST,OPTIONS,TRACE
Content-Type: httpd/unix-directory
</pre>
<h2>TRACE Method</h2>
<p>The TRACE method is used to echo the contents of an HTTP Request back to the requester which can be used for debugging purpose at the time of development. The following example shows the usage of TRACE method:</p>
<pre class="result notranslate">
TRACE / HTTP/1.1
Host: www.tutorialspoint.com
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
</pre>
<p>The server will send the following message in response to the above request:</p>
<pre class="result notranslate">
HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Connection: close