]> gitweb.factorcode.org Git - factor.git/blob - basis/http/client/client-docs.factor
http.client: remove http-get* and its friends, change http-request* and with-http...
[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" } " 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: <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: http-request
89 { $values { "request" request } { "response" response } { "data" sequence } }
90 { $description "A variant of " { $link http-request* } " that checks that the response was successful." }
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
97 HELP: with-http-request
98 { $values { "request" request } { "quot" { $quotation "( chunk -- )" } } { "response" response } }
99 { $description "A variant of " { $link with-http-request* } " that checks that the response was successful." } ;
100
101 HELP: with-http-request*
102 { $values { "request" request } { "quot" { $quotation "( chunk -- )" } } { "response" response } }
103 { $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" } "." } ;
104
105 { http-request http-request* with-http-request with-http-request* } related-words
106
107 ARTICLE: "http.client.get" "GET requests with the HTTP client"
108 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
109 { $subsections
110     http-get
111 }
112 "Utilities to retrieve a " { $link url } " and save the contents to a file:"
113 { $subsections
114     download
115     download-to
116 }
117 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
118 { $subsections
119     <get-request>
120     http-request
121     http-request*
122 }
123 "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:"
124 { $subsections
125     with-http-request
126     with-http-request*
127 } ;
128
129 ARTICLE: "http.client.post-data" "HTTP client post data"
130 "HTTP POST and PUT request words take a post data parameter, which can be one of the following:"
131 { $list
132     { "a " { $link byte-array } ": the data is sent the server without further encoding" }
133     { "a " { $link string } ": the data is encoded and then sent as a series of bytes" }
134     { "an " { $link assoc } ": the assoc is interpreted as a series of form parameters, which are encoded with " { $link assoc>query } }
135     { "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" }
136     { { $link f } " denotes that there is no post data" }
137     { "a " { $link post-data } " tuple, for additional control" }
138 }
139 "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,"
140 { $code
141   "\"my-large-post-request.txt\" ascii <file-reader>"
142   "[ URL\" http://www.my-company.com/web-service\" http-post ] with-disposal"
143 }
144 "An internal word used to convert objects to " { $link post-data } " instances:"
145 { $subsections >post-data } ;
146
147 ARTICLE: "http.client.post" "POST requests with the HTTP client"
148 "Basic usage involves passing post data and a " { $link url } ", and getting a " { $link response } " and data back:"
149 { $subsections http-post }
150 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
151 { $subsections <post-request> }
152 "Both words take a post data parameter; see " { $link "http.client.post-data" } "." ;
153
154 ARTICLE: "http.client.put" "PUT requests with the HTTP client"
155 "Basic usage involves passing post data and a " { $link url } ", and getting a " { $link response } " and data back:"
156 { $subsections http-put }
157 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
158 { $subsections <put-request> }
159 "Both words take a post data parameter; see " { $link "http.client.post-data" } "." ;
160
161 ARTICLE: "http.client.head" "HEAD requests with the HTTP client"
162 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
163 { $subsections http-head }
164 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
165 { $subsections
166     <head-request>
167 } ;
168
169 ARTICLE: "http.client.delete" "DELETE requests with the HTTP client"
170 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
171 { $subsections http-delete }
172 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
173 { $subsections
174     <delete-request>
175 } ;
176
177 ARTICLE: "http.client.options" "OPTIONS requests with the HTTP client"
178 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
179 { $subsections http-options }
180 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
181 { $subsections
182     <options-request>
183 }
184 "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." ;
185
186 ARTICLE: "http.client.trace" "TRACE requests with the HTTP client"
187 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
188 { $subsections http-trace }
189 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
190 { $subsections
191     <trace-request>
192 } ;
193
194 ARTICLE: "http.client.encoding" "Character encodings and the HTTP client"
195 "The " { $link http-request } ", " { $link http-get } " and " { $link http-post } " words output a sequence containing data that was sent by the server."
196 $nl
197 "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."
198 $nl
199 "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."
200 $nl
201 "For any other MIME type, the " { $link binary } " encoding is assumed, and thus the data is returned literally in a byte array." ;
202
203 ARTICLE: "http.client.errors" "HTTP client errors"
204 "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:"
205 { $subsections
206     download-failed
207     too-many-redirects
208 } ;
209
210 ARTICLE: "http.client" "HTTP client"
211 "The " { $vocab-link "http.client" } " vocabulary implements an HTTP and HTTPS client on top of " { $link "http" } "."
212 $nl
213 "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" } "."
214 $nl
215 "There are two primary usage patterns, data retrieval with GET requests and form submission with POST requests:"
216 { $subsections
217     "http.client.get"
218     "http.client.post"
219     "http.client.put"
220 }
221 "Submission data for POST and PUT requests:"
222 { $subsections "http.client.post-data" }
223 "Other HTTP methods are also supported:"
224 { $subsections
225     "http.client.head"
226     "http.client.delete"
227     "http.client.options"
228     "http.client.trace"
229 }
230 "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."
231 { $subsections
232     "http.client.encoding"
233     "http.client.errors"
234 }
235 { $see-also "urls" } ;
236
237 ABOUT: "http.client"