]> gitweb.factorcode.org Git - factor.git/blob - basis/http/client/client-docs.factor
d846693adb938b67e85d04a6530712ee68513b6a
[factor.git] / basis / http / client / client-docs.factor
1 USING: assocs byte-arrays destructors help.markup help.syntax http
2 http.client.post-data.private http.client.private
3 io.encodings.binary io.encodings.latin1 io.pathnames kernel
4 sequences strings urls urls.encoding ;
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" } " slot 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: invalid-proxy
14 { $error-description "Thrown by " { $link http-request } " if the proxy url is not valid." } ;
15
16 HELP: <get-request>
17 { $values { "url" { $or url string } } { "request" request } }
18 { $description "Constructs an HTTP GET request for retrieving the URL." }
19 { $notes "The request can be passed on to " { $link http-request } ", possibly after cookies and headers are set." } ;
20
21 HELP: <post-request>
22 { $values { "post-data" object } { "url" { $or url string } } { "request" request } }
23 { $description "Constructs an HTTP POST request for submitting post data to the URL." }
24 { $notes "The request can be passed on to " { $link http-request } ", possibly after cookies and headers are set." } ;
25
26 HELP: <head-request>
27 { $values { "url" { $or url string } } { "request" request } }
28 { $description "Constructs an HTTP HEAD request for retrieving the URL." }
29 { $notes "The request can be passed on to " { $link http-request } ", possibly after cookies and headers are set." } ;
30
31 HELP: <delete-request>
32 { $values { "url" { $or url string } } { "request" request } }
33 { $description "Constructs an HTTP DELETE request for the requested URL." }
34 { $notes "The request can be passed on to " { $link http-request } ", possibly after cookies and headers are set." } ;
35
36 HELP: <options-request>
37 { $values { "url" { $or url string } } { "request" request } }
38 { $description "Constructs an HTTP OPTIONS request for the requested URL." }
39 { $notes "The request can be passed on to " { $link http-request } ", possibly after cookies and headers are set." } ;
40
41 HELP: <trace-request>
42 { $values { "url" { $or url string } } { "request" request } }
43 { $description "Constructs an HTTP TRACE request for the requested URL." }
44 { $notes "The request can be passed on to " { $link http-request } ", possibly after cookies and headers are set." } ;
45
46 HELP: download
47 { $values { "url" { $or url string } } }
48 { $description "Downloads the contents of the URL to a file in the " { $link current-directory } " having the same file name." }
49 { $errors "Throws an error if the HTTP request fails." } ;
50
51 HELP: download-to
52 { $values { "url" { $or url string } } { "file" "a pathname string" } }
53 { $description "Downloads the contents of the URL to a file with the given pathname." }
54 { $errors "Throws an error if the HTTP request fails." } ;
55
56 HELP: ?download-to
57 { $values { "url" { $or url string } } { "file" "a pathname string" } }
58 { $description "Version of " { $link download-to } " that only downloads if " { $snippet "file" } " does not exist." }
59 { $errors "Throws an error if the HTTP request fails." } ;
60
61 HELP: http-get
62 { $values { "url" { $or url string } } { "response" response } { "data" sequence } }
63 { $description "Downloads the contents of a URL." }
64 { $errors "Throws an error if the HTTP request fails." } ;
65
66 HELP: http-get*
67 { $values { "url" { $or url string } } { "response" response } { "data" sequence } }
68 { $description "Downloads the contents of a URL, but does not check the HTTP response code for success." } ;
69
70 { http-get http-get* } related-words
71
72 HELP: http-post
73 { $values { "post-data" object } { "url" { $or url string } } { "response" response } { "data" sequence } }
74 { $description "Submits an HTTP POST request." }
75 { $errors "Throws an error if the HTTP request fails." } ;
76
77 HELP: http-post*
78 { $values { "post-data" object } { "url" { $or url string } } { "response" response } { "data" sequence } }
79 { $description "Submits an HTTP POST request, but does not check the HTTP response code for success." } ;
80
81 { http-post http-post* } related-words
82
83 HELP: http-put
84 { $values { "post-data" object } { "url" { $or url string } } { "response" response } { "data" sequence } }
85 { $description "Submits an HTTP PUT request." }
86 { $errors "Throws an error if the HTTP request fails." } ;
87
88 HELP: http-put*
89 { $values { "post-data" object } { "url" { $or url string } } { "response" response } { "data" sequence } }
90 { $description "Submits an HTTP PUT request, but does not check the HTTP response code for success." } ;
91
92 { http-put http-put* } related-words
93
94 HELP: http-head
95 { $values { "url" { $or url string } } { "response" response } { "data" sequence } }
96 { $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." }
97 { $errors "Throws an error if the HTTP request fails." } ;
98
99 HELP: http-head*
100 { $values { "url" { $or url string } } { "response" response } { "data" sequence } }
101 { $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." } ;
102
103 { http-head http-head* } related-words
104
105 HELP: http-delete
106 { $values { "url" { $or url string } } { "response" response } { "data" sequence } }
107 { $description "Requests that the origin server delete the resource identified by the URL." }
108 { $errors "Throws an error if the HTTP request fails." } ;
109
110 HELP: http-delete*
111 { $values { "url" { $or url string } } { "response" response } { "data" sequence } }
112 { $description "Requests that the origin server delete the resource identified by the URL, but does not check the HTTP response code for success." } ;
113
114 { http-delete http-delete* } related-words
115
116 HELP: http-options
117 { $values { "url" { $or url string } } { "response" response } { "data" sequence } }
118 { $description "Submits an HTTP OPTIONS request." }
119 { $errors "Throws an error if the HTTP request fails." } ;
120
121 HELP: http-options*
122 { $values { "url" { $or url string } } { "response" response } { "data" sequence } }
123 { $description "Submits an HTTP OPTIONS request, but does not check the HTTP response code for success." } ;
124
125 { http-options http-options* } related-words
126
127 HELP: http-trace
128 { $values { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
129 { $description "Submits an HTTP TRACE request." }
130 { $errors "Throws an error if the HTTP request fails." } ;
131
132 HELP: http-trace*
133 { $values { "url" { $or url string } } { "response" response } { "data" sequence } }
134 { $description "Submits an HTTP TRACE request, but does not check the HTTP response code for success." } ;
135
136 { http-trace http-trace* } related-words
137
138 HELP: http-request
139 { $values { "request" request } { "response" response } { "data" sequence } }
140 { $description "A variant of " { $link http-request* } " that checks that the response was successful." }
141 { $errors "Throws an error if the HTTP request fails." } ;
142
143 HELP: http-request*
144 { $values { "request" request } { "response" response } { "data" sequence } }
145 { $description "Sends an HTTP request to an HTTP server, and reads the response." } ;
146
147 HELP: read-response-header
148 { $values { "response" response } }
149 { $description "Initializes the 'header', 'cookies', 'content-type', 'content-charset' and 'content-encoding' field of the response." } ;
150
151 HELP: with-http-request
152 { $values { "request" request } { "quot" { $quotation ( chunk -- ) } } { "response" response } }
153 { $description "A variant of " { $link with-http-request* } " that checks that the response was successful." } ;
154
155 HELP: with-http-request*
156 { $values { "request" request } { "quot" { $quotation ( chunk -- ) } } { "response" response } }
157 { $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" } "." } ;
158
159 { http-request http-request* with-http-request with-http-request* } related-words
160
161 ARTICLE: "http.client.get" "GET requests with the HTTP client"
162 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
163 { $subsections
164     http-get
165     http-get*
166 }
167 "Utilities to retrieve a " { $link url } " and save the contents to a file:"
168 { $subsections
169     download
170     download-to
171     ?download-to
172 }
173 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
174 { $subsections
175     <get-request>
176     http-request
177     http-request*
178 }
179 "The " { $link http-request } " 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:"
180 { $subsections
181     with-http-request
182     with-http-request*
183 } ;
184
185 ARTICLE: "http.client.post-data" "HTTP client post data"
186 "HTTP POST and PUT request words take a post data parameter, which can be one of the following:"
187 { $list
188     { "a " { $link byte-array } ": the data is sent the server without further encoding" }
189     { "a " { $link string } ": the data is encoded and then sent as a series of bytes" }
190     { "an " { $link assoc } ": the assoc is interpreted as a series of form parameters, which are encoded with " { $link assoc>query } }
191     { "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" }
192     { { $link f } " denotes that there is no post data" }
193     { "a " { $link post-data } " tuple, for additional control" }
194 }
195 "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,"
196 { $code
197   "\"my-large-post-request.txt\" ascii <file-reader>"
198   "[ URL\" http://www.my-company.com/web-service\" http-post ] with-disposal"
199 }
200 "An internal word used to convert objects to " { $link post-data } " instances:"
201 { $subsections >post-data } ;
202
203 ARTICLE: "http.client.post" "POST requests with the HTTP client"
204 "Basic usage involves passing post data and a " { $link url } ", and getting a " { $link response } " and data back:"
205 { $subsections http-post http-post* }
206 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
207 { $subsections <post-request> }
208 "Both words take a post data parameter; see " { $link "http.client.post-data" } "." ;
209
210 ARTICLE: "http.client.put" "PUT requests with the HTTP client"
211 "Basic usage involves passing post data and a " { $link url } ", and getting a " { $link response } " and data back:"
212 { $subsections http-put http-put* }
213 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
214 { $subsections <put-request> }
215 "Both words take a post data parameter; see " { $link "http.client.post-data" } "." ;
216
217 ARTICLE: "http.client.head" "HEAD requests with the HTTP client"
218 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
219 { $subsections http-head http-head* }
220 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
221 { $subsections
222     <head-request>
223 } ;
224
225 ARTICLE: "http.client.delete" "DELETE requests with the HTTP client"
226 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
227 { $subsections http-delete http-delete* }
228 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
229 { $subsections
230     <delete-request>
231 } ;
232
233 ARTICLE: "http.client.options" "OPTIONS requests with the HTTP client"
234 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
235 { $subsections http-options http-options* }
236 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
237 { $subsections
238     <options-request>
239 }
240 "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." ;
241
242 ARTICLE: "http.client.trace" "TRACE requests with the HTTP client"
243 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
244 { $subsections http-trace http-trace* }
245 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
246 { $subsections
247     <trace-request>
248 } ;
249
250 ARTICLE: "http.client.encoding" "Character encodings and the HTTP client"
251 "The " { $link http-request } ", " { $link http-get } " and " { $link http-post } " words output a sequence containing data that was sent by the server."
252 $nl
253 "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."
254 $nl
255 "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."
256 $nl
257 "For any other MIME type, the " { $link binary } " encoding is assumed, and thus the data is returned literally in a byte array." ;
258
259 ARTICLE: "http.client.errors" "HTTP client errors"
260 "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:"
261 { $subsections
262     download-failed
263     too-many-redirects
264 } ;
265
266 ARTICLE: "http.client" "HTTP client"
267 "The " { $vocab-link "http.client" } " vocabulary implements an HTTP and HTTPS client on top of " { $link "http" } "."
268 $nl
269 "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" } "."
270 $nl
271 "There are two primary usage patterns, data retrieval with GET requests and form submission with POST requests:"
272 { $subsections
273     "http.client.get"
274     "http.client.post"
275     "http.client.put"
276 }
277 "Submission data for POST and PUT requests:"
278 { $subsections "http.client.post-data" }
279 "Other HTTP methods are also supported:"
280 { $subsections
281     "http.client.head"
282     "http.client.delete"
283     "http.client.options"
284     "http.client.trace"
285 }
286 "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."
287 { $subsections
288     "http.client.encoding"
289     "http.client.errors"
290 }
291 "For authentication, only Basic Access Authentication is implemented, using the username/password from the target or proxy url. Alternatively, the " { $link set-basic-auth } " or " { $link set-proxy-basic-auth } " words can be called on the " { $link request } " object."
292 $nl
293 "The http client can use an HTTP proxy transparently, by using the " { $link "http.proxy-variables" } ". Additionally, the proxy variables can be ignored by setting the " { $slot "proxy-url" } " slot of each " { $link request } " manually:"
294 { $list
295     { "Setting " { $slot "proxy-url" } " to " { $link f } " prevents http.client from using a proxy." }
296     { "Setting the slots of the default empty url in " { $slot "proxy-url" } " overrides the corresponding values from the proxy variables." }
297 }
298
299 { $see-also "urls" } ;
300
301 ABOUT: "http.client"