]> gitweb.factorcode.org Git - factor.git/blob - basis/http/client/client-docs.factor
add PATCH http method
[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-patch
128 { $values { "url" { $or url string } } { "response" response } { "data" sequence } }
129 { $description "Submits an HTTP PATCH request." }
130 { $errors "Throws an error if the HTTP request fails." } ;
131
132 HELP: http-patch*
133 { $values { "url" { $or url string } } { "response" response } { "data" sequence } }
134 { $description "Submits an HTTP PATCH request, but does not check the HTTP response code for success." } ;
135
136 { http-patch http-patch* } related-words
137
138 HELP: http-trace
139 { $values { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
140 { $description "Submits an HTTP TRACE request." }
141 { $errors "Throws an error if the HTTP request fails." } ;
142
143 HELP: http-trace*
144 { $values { "url" { $or url string } } { "response" response } { "data" sequence } }
145 { $description "Submits an HTTP TRACE request, but does not check the HTTP response code for success." } ;
146
147 { http-trace http-trace* } related-words
148
149 HELP: http-request
150 { $values { "request" request } { "response" response } { "data" sequence } }
151 { $description "A variant of " { $link http-request* } " that checks that the response was successful." }
152 { $errors "Throws an error if the HTTP request fails." } ;
153
154 HELP: http-request*
155 { $values { "request" request } { "response" response } { "data" sequence } }
156 { $description "Sends an HTTP request to an HTTP server, and reads the response." } ;
157
158 HELP: read-response-header
159 { $values { "response" response } }
160 { $description "Initializes the 'header', 'cookies', 'content-type', 'content-charset' and 'content-encoding' field of the response." } ;
161
162 HELP: with-http-request
163 { $values { "request" request } { "quot" { $quotation ( chunk -- ) } } { "response" response } }
164 { $description "A variant of " { $link with-http-request* } " that checks that the response was successful." } ;
165
166 HELP: with-http-request*
167 { $values { "request" request } { "quot" { $quotation ( chunk -- ) } } { "response" response } }
168 { $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" } "." } ;
169
170 { http-request http-request* with-http-request with-http-request* } related-words
171
172 ARTICLE: "http.client.get" "GET requests with the HTTP client"
173 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
174 { $subsections
175     http-get
176     http-get*
177 }
178 "Utilities to retrieve a " { $link url } " and save the contents to a file:"
179 { $subsections
180     download
181     download-to
182     ?download-to
183 }
184 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
185 { $subsections
186     <get-request>
187     http-request
188     http-request*
189 }
190 "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:"
191 { $subsections
192     with-http-request
193     with-http-request*
194 } ;
195
196 ARTICLE: "http.client.post-data" "HTTP client post data"
197 "HTTP POST and PUT request words take a post data parameter, which can be one of the following:"
198 { $list
199     { "a " { $link byte-array } ": the data is sent the server without further encoding" }
200     { "a " { $link string } ": the data is encoded and then sent as a series of bytes" }
201     { "an " { $link assoc } ": the assoc is interpreted as a series of form parameters, which are encoded with " { $link assoc>query } }
202     { "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" }
203     { { $link f } " denotes that there is no post data" }
204     { "a " { $link post-data } " tuple, for additional control" }
205 }
206 "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,"
207 { $code
208   "\"my-large-post-request.txt\" ascii <file-reader>"
209   "[ URL\" http://www.my-company.com/web-service\" http-post ] with-disposal"
210 }
211 "An internal word used to convert objects to " { $link post-data } " instances:"
212 { $subsections >post-data } ;
213
214 ARTICLE: "http.client.post" "POST requests with the HTTP client"
215 "Basic usage involves passing post data and a " { $link url } ", and getting a " { $link response } " and data back:"
216 { $subsections http-post http-post* }
217 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
218 { $subsections <post-request> }
219 "Both words take a post data parameter; see " { $link "http.client.post-data" } "." ;
220
221 ARTICLE: "http.client.put" "PUT requests with the HTTP client"
222 "Basic usage involves passing post data and a " { $link url } ", and getting a " { $link response } " and data back:"
223 { $subsections http-put http-put* }
224 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
225 { $subsections <put-request> }
226 "Both words take a post data parameter; see " { $link "http.client.post-data" } "." ;
227
228 ARTICLE: "http.client.head" "HEAD requests with the HTTP client"
229 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
230 { $subsections http-head http-head* }
231 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
232 { $subsections
233     <head-request>
234 } ;
235
236 ARTICLE: "http.client.delete" "DELETE requests with the HTTP client"
237 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
238 { $subsections http-delete http-delete* }
239 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
240 { $subsections
241     <delete-request>
242 } ;
243
244 ARTICLE: "http.client.options" "OPTIONS requests with the HTTP client"
245 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
246 { $subsections http-options http-options* }
247 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
248 { $subsections
249     <options-request>
250 }
251 "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." ;
252
253 ARTICLE: "http.client.trace" "TRACE requests with the HTTP client"
254 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
255 { $subsections http-trace http-trace* }
256 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
257 { $subsections
258     <trace-request>
259 } ;
260
261 ARTICLE: "http.client.encoding" "Character encodings and the HTTP client"
262 "The " { $link http-request } ", " { $link http-get } " and " { $link http-post } " words output a sequence containing data that was sent by the server."
263 $nl
264 "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."
265 $nl
266 "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."
267 $nl
268 "For any other MIME type, the " { $link binary } " encoding is assumed, and thus the data is returned literally in a byte array." ;
269
270 ARTICLE: "http.client.errors" "HTTP client errors"
271 "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:"
272 { $subsections
273     download-failed
274     too-many-redirects
275 } ;
276
277 ARTICLE: "http.client" "HTTP client"
278 "The " { $vocab-link "http.client" } " vocabulary implements an HTTP and HTTPS client on top of " { $link "http" } "."
279 $nl
280 "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" } "."
281 $nl
282 "There are two primary usage patterns, data retrieval with GET requests and form submission with POST requests:"
283 { $subsections
284     "http.client.get"
285     "http.client.post"
286     "http.client.put"
287 }
288 "Submission data for POST and PUT requests:"
289 { $subsections "http.client.post-data" }
290 "Other HTTP methods are also supported:"
291 { $subsections
292     "http.client.head"
293     "http.client.delete"
294     "http.client.options"
295     "http.client.trace"
296 }
297 "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."
298 { $subsections
299     "http.client.encoding"
300     "http.client.errors"
301 }
302 "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."
303 $nl
304 "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:"
305 { $list
306     { "Setting " { $slot "proxy-url" } " to " { $link f } " prevents http.client from using a proxy." }
307     { "Setting the slots of the default empty url in " { $slot "proxy-url" } " overrides the corresponding values from the proxy variables." }
308 }
309
310 { $see-also "urls" } ;
311
312 ABOUT: "http.client"