]> gitweb.factorcode.org Git - factor.git/blob - basis/http/client/client-docs.factor
Spelling fixes to documentation.
[factor.git] / basis / http / client / client-docs.factor
1 USING: http help.markup help.syntax io.pathnames io.streams.string
2 io.encodings.binary kernel urls
3 urls.encoding byte-arrays strings assocs sequences destructors
4 http.client.post-data.private io.encodings.8-bit.latin1 ;
5 IN: http.client
6
7 HELP: download-failed
8 { $error-description "Thrown by " { $link http-request } " if the server returns a status code other than 200. The " { $slot "response" } " and " { $slot "body" } " slots can be inspected for the underlying cause of the problem." } ;
9
10 HELP: too-many-redirects
11 { $error-description "Thrown by " { $link http-request } " if the server returns a chain of than " { $link max-redirects } " redirections." } ;
12
13 HELP: <get-request>
14 { $values { "url" "a " { $link url } " or " { $link string } } { "request" request } }
15 { $description "Constructs an HTTP GET request for retrieving the URL." }
16 { $notes "The request can be passed on to " { $link http-request } ", possibly after cookies and headers are set." } ;
17
18 HELP: <post-request>
19 { $values { "post-data" object } { "url" "a " { $link url } " or " { $link string } } { "request" request } }
20 { $description "Constructs an HTTP POST request for submitting post data to the URL." }
21 { $notes "The request can be passed on to " { $link http-request } ", possibly after cookies and headers are set." } ;
22
23 HELP: <head-request>
24 { $values { "url" "a " { $link url } " or " { $link string } } { "request" request } }
25 { $description "Constructs an HTTP HEAD request for retrieving the URL." }
26 { $notes "The request can be passed on to " { $link http-request } ", possibly after cookies and headers are set." } ;
27
28 HELP: <delete-request>
29 { $values { "url" "a " { $link url } " or " { $link string } } { "request" request } }
30 { $description "Constructs an HTTP DELETE request for the requested URL." }
31 { $notes "The request can be passed on to " { $link http-request } ", possibly after cookies and headers are set." } ;
32
33 HELP: <options-request>
34 { $values { "url" "a " { $link url } " or " { $link string } } { "request" request } }
35 { $description "Constructs an HTTP OPTIONS request for the requested URL." }
36 { $notes "The request can be passed on to " { $link http-request } ", possibly after cookies and headers are set." } ;
37
38 HELP: <trace-request>
39 { $values { "url" "a " { $link url } " or " { $link string } } { "request" request } }
40 { $description "Constructs an HTTP TRACE request for the requested URL." }
41 { $notes "The request can be passed on to " { $link http-request } ", possibly after cookies and headers are set." } ;
42
43 HELP: download
44 { $values { "url" "a " { $link url } " or " { $link string } } }
45 { $description "Downloads the contents of the URL to a file in the " { $link current-directory } " having the same file name." }
46 { $errors "Throws an error if the HTTP request fails." } ;
47
48 HELP: download-to
49 { $values { "url" "a " { $link url } " or " { $link string } } { "file" "a pathname string" } }
50 { $description "Downloads the contents of the URL to a file with the given pathname." }
51 { $errors "Throws an error if the HTTP request fails." } ;
52
53 HELP: http-get
54 { $values { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
55 { $description "Downloads the contents of a URL." }
56 { $errors "Throws an error if the HTTP request fails." } ;
57
58 HELP: http-post
59 { $values { "post-data" object } { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
60 { $description "Submits an HTTP POST request." }
61 { $errors "Throws an error if the HTTP request fails." } ;
62
63 HELP: http-put
64 { $values { "post-data" object } { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
65 { $description "Submits an HTTP PUT request." }
66 { $errors "Throws an error if the HTTP request fails." } ;
67
68 HELP: http-head
69 { $values { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
70 { $description "Same as " { $link http-get } " except that the server is not supposed to return a message-body in the response, as per RFC2616. However in practise, most web servers respond to GET and HEAD method calls with identical responses." }
71 { $errors "Throws an error if the HTTP request fails." } ;
72  
73 HELP: http-delete
74 { $values { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
75 { $description "Requests that the origin server delete the resource identified by the URL." }
76 { $errors "Throws an error if the HTTP request fails." } ;
77
78 HELP: http-options
79 { $values { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
80 { $description "Submits an HTTP OPTIONS request." }
81 { $errors "Throws an error if the HTTP request fails." } ;
82
83 HELP: http-trace
84 { $values { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
85 { $description "Submits an HTTP TRACE request." }
86 { $errors "Throws an error if the HTTP request fails." } ;
87
88 HELP: with-http-get
89 { $values { "url" "a " { $link url } " or " { $link string } } { "quot" { $quotation "( chunk -- )" } } { "response" response } }
90 { $description "Downloads the contents of a URL. Chunks of data are passed to the quotation as they are read." }
91 { $errors "Throws an error if the HTTP request fails." } ;
92
93 HELP: http-request
94 { $values { "request" request } { "response" response } { "data" sequence } }
95 { $description "Sends an HTTP request to an HTTP server, and reads the response." }
96 { $errors "Throws an error if the HTTP request fails." } ;
97
98 HELP: with-http-request
99 { $values { "request" request } { "quot" { $quotation "( chunk -- )" } } { "response" response } }
100 { $description "Sends an HTTP request to an HTTP server, and reads the response incrementally. Chunks of data are passed to the quotation as they are read. Does not throw an error if the HTTP request fails; to do so, call " { $link check-response } " on the " { $snippet "response" } "." } ;
101
102 ARTICLE: "http.client.get" "GET requests with the HTTP client"
103 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
104 { $subsections http-get }
105 "Utilities to retrieve a " { $link url } " and save the contents to a file:"
106 { $subsections
107     download
108     download-to
109 }
110 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
111 { $subsections
112     <get-request>
113     http-request
114 }
115 "The " { $link http-get } " and " { $link http-request } " words output sequences. This is undesirable if the response data may be large. Another pair of words take a quotation instead, and pass the quotation chunks of data incrementally:"
116 { $subsections
117     with-http-get
118     with-http-request
119 } ;
120
121 ARTICLE: "http.client.post-data" "HTTP client post data"
122 "HTTP POST and PUT request words take a post data parameter, which can be one of the following:"
123 { $list
124     { "a " { $link byte-array } ": the data is sent the server without further encoding" }
125     { "a " { $link string } ": the data is encoded and then sent as a series of bytes" }
126     { "an " { $link assoc } ": the assoc is interpreted as a series of form parameters, which are encoded with " { $link assoc>query } }
127     { "an input stream: the contents of the input stream are transmitted to the server without being read entirely into memory - this is useful for large requests" }
128     { { $link f } " denotes that there is no post data" }
129     { "a " { $link post-data } " tuple, for additional control" }
130 }
131 "When passing a stream, you must ensure the stream is closed afterwards. The best way is to use " { $link with-disposal } " or " { $link "destructors" } ". For example,"
132 { $code
133   "\"my-large-post-request.txt\" ascii <file-reader>"
134   "[ URL\" http://www.my-company.com/web-service\" http-post ] with-disposal"
135 }
136 "An internal word used to convert objects to " { $link post-data } " instances:"
137 { $subsections >post-data } ;
138
139 ARTICLE: "http.client.post" "POST requests with the HTTP client"
140 "Basic usage involves passing post data and a " { $link url } ", and getting a " { $link response } " and data back:"
141 { $subsections http-post }
142 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
143 { $subsections <post-request> }
144 "Both words take a post data parameter; see " { $link "http.client.post-data" } "." ;
145
146 ARTICLE: "http.client.put" "PUT requests with the HTTP client"
147 "Basic usage involves passing post data and a " { $link url } ", and getting a " { $link response } " and data back:"
148 { $subsections http-post }
149 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
150 { $subsections <post-request> }
151 "Both words take a post data parameter; see " { $link "http.client.post-data" } "." ;
152
153 ARTICLE: "http.client.head" "HEAD requests with the HTTP client"
154 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
155 { $subsections http-head }
156 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
157 { $subsections
158     <head-request>
159 } ;
160
161 ARTICLE: "http.client.delete" "DELETE requests with the HTTP client"
162 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
163 { $subsections http-delete }
164 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
165 { $subsections
166     <delete-request>
167 } ;
168
169 ARTICLE: "http.client.options" "OPTIONS requests with the HTTP client"
170 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
171 { $subsections http-options }
172 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
173 { $subsections
174     <options-request>
175 }
176 "RFC2616 does not define any use for an entity body, yet allows for the inclusion of one as part of the OPTIONS method. This is not supported with this version of the " { $vocab-link "http.client" } ". The current implementation of " { $link http-options } " only supports a " { $link url } " request with no corresponding post-data, as per the stack effect." ;
177
178 ARTICLE: "http.client.trace" "TRACE requests with the HTTP client"
179 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
180 { $subsections http-trace }
181 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
182 { $subsections
183     <trace-request>
184 } ;
185
186 ARTICLE: "http.client.encoding" "Character encodings and the HTTP client"
187 "The " { $link http-request } ", " { $link http-get } " and " { $link http-post } " words output a sequence containing data that was sent by the server."
188 $nl
189 "If the server specifies a " { $snippet "content-type" } " header with a character encoding, the HTTP client decodes the data using this character encoding, and the sequence will be a string."
190 $nl
191 "If no encoding was specified but the MIME type is a text type, the " { $link latin1 } " encoding is assumed, and the sequence will be a string."
192 $nl
193 "For any other MIME type, the " { $link binary } " encoding is assumed, and thus the data is returned literally in a byte array." ;
194
195 ARTICLE: "http.client.errors" "HTTP client errors"
196 "HTTP operations may fail for one of two reasons. The first is an I/O error resulting from a network problem; a name server lookup failure, or a refused connection. The second is a protocol-level error returned by the server. There are two such errors:"
197 { $subsections
198     download-failed
199     too-many-redirects
200 } ;
201
202 ARTICLE: "http.client" "HTTP client"
203 "The " { $vocab-link "http.client" } " vocabulary implements an HTTP and HTTPS client on top of " { $link "http" } "."
204 $nl
205 "For HTTPS support, you must load the " { $vocab-link "io.sockets.secure" } " vocab first. If you don't need HTTPS support, don't load " { $vocab-link "io.sockets.secure" } "; this will reduce the size of images generated by " { $vocab-link "tools.deploy" } "."
206 $nl
207 "There are two primary usage patterns, data retrieval with GET requests and form submission with POST requests:"
208 { $subsections
209     "http.client.get"
210     "http.client.post"
211     "http.client.put"
212 }
213 "Submission data for POST and PUT requests:"
214 { $subsections "http.client.post-data" }
215 "Other HTTP methods are also supported:"
216 { $subsections
217     "http.client.head"
218     "http.client.delete"
219     "http.client.options"
220     "http.client.trace"
221 }
222 "More esoteric use-cases, for example HTTP methods other than the above, are accommodated by constructing an empty request object with " { $link <request> } " and filling everything in by hand."
223 { $subsections
224     "http.client.encoding"
225     "http.client.errors"
226 }
227 { $see-also "urls" } ;
228
229 ABOUT: "http.client"