]> gitweb.factorcode.org Git - factor.git/blob - basis/http/client/client-docs.factor
890518aa2ab1aab493ee888b124b400f4a71b8ba
[factor.git] / basis / http / client / client-docs.factor
1 USING: http help.markup help.syntax io.pathnames io.streams.string
2 io.encodings.8-bit io.encodings.binary kernel urls
3 urls.encoding byte-arrays strings assocs sequences destructors
4 http.client.post-data.private ;
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: download
24 { $values { "url" "a " { $link url } " or " { $link string } } }
25 { $description "Downloads the contents of the URL to a file in the " { $link current-directory } " having the same file name." }
26 { $errors "Throws an error if the HTTP request fails." } ;
27
28 HELP: download-to
29 { $values { "url" "a " { $link url } " or " { $link string } } { "file" "a pathname string" } }
30 { $description "Downloads the contents of the URL to a file with the given pathname." }
31 { $errors "Throws an error if the HTTP request fails." } ;
32
33 HELP: http-get
34 { $values { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
35 { $description "Downloads the contents of a URL." }
36 { $errors "Throws an error if the HTTP request fails." } ;
37
38 HELP: http-post
39 { $values { "post-data" object } { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
40 { $description "Submits an HTTP POST request." }
41 { $errors "Throws an error if the HTTP request fails." } ;
42
43 HELP: http-put
44 { $values { "post-data" object } { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
45 { $description "Submits an HTTP PUT request." }
46 { $errors "Throws an error if the HTTP request fails." } ;
47
48 HELP: with-http-get
49 { $values { "url" "a " { $link url } " or " { $link string } } { "quot" { $quotation "( chunk -- )" } } { "response" response } }
50 { $description "Downloads the contents of a URL. Chunks of data are passed to the quotation as they are read." }
51 { $errors "Throws an error if the HTTP request fails." } ;
52
53 HELP: http-request
54 { $values { "request" request } { "response" response } { "data" sequence } }
55 { $description "Sends an HTTP request to an HTTP server, and reads the response." }
56 { $errors "Throws an error if the HTTP request fails." } ;
57
58 HELP: with-http-request
59 { $values { "request" request } { "quot" { $quotation "( chunk -- )" } } { "response" response } }
60 { $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" } "." } ;
61
62 ARTICLE: "http.client.get" "GET requests with the HTTP client"
63 "Basic usage involves passing a " { $link url } " and getting a " { $link response } " and data back:"
64 { $subsection http-get }
65 "Utilities to retrieve a " { $link url } " and save the contents to a file:"
66 { $subsection download }
67 { $subsection download-to }
68 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
69 { $subsection <get-request> }
70 { $subsection http-request }
71 "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:"
72 { $subsection with-http-get }
73 { $subsection with-http-request } ;
74
75 ARTICLE: "http.client.post-data" "HTTP client post data"
76 "HTTP POST and PUT request words take a post data parameter, which can be one of the following:"
77 { $list
78     { "a " { $link byte-array } ": the data is sent the server without further encoding" }
79     { "a " { $link string } ": the data is encoded and then sent as a series of bytes" }
80     { "an " { $link assoc } ": the assoc is interpreted as a series of form parameters, which are encoded with " { $link assoc>query } }
81     { "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" }
82     { { $link f } " denotes that there is no post data" }
83     { "a " { $link post-data } " tuple, for additional control" }
84 }
85 "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,"
86 { $code
87   "\"my-large-post-request.txt\" ascii <file-reader>"
88   "[ URL\" http://www.my-company.com/web-service\" http-post ] with-disposal"
89 }
90 "An internal word used to convert objects to " { $link post-data } " instances:"
91 { $subsection >post-data } ;
92
93 ARTICLE: "http.client.post" "POST requests with the HTTP client"
94 "Basic usage involves passing post data and a " { $link url } ", and getting a " { $link response } " and data back:"
95 { $subsection http-post }
96 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
97 { $subsection <post-request> }
98 "Both words take a post data parameter; see " { $link "http.client.post-data" } "." ;
99
100 ARTICLE: "http.client.put" "PUT requests with the HTTP client"
101 "Basic usage involves passing post data and a " { $link url } ", and getting a " { $link response } " and data back:"
102 { $subsection http-post }
103 "Advanced usage involves constructing a " { $link request } ", which allows " { $link "http.cookies" } " and " { $link "http.headers" } " to be set:"
104 { $subsection <post-request> }
105 "Both words take a post data parameter; see " { $link "http.client.post-data" } "." ;
106
107 ARTICLE: "http.client.encoding" "Character encodings and the HTTP client"
108 "The " { $link http-request } ", " { $link http-get } " and " { $link http-post } " words output a sequence containing data that was sent by the server."
109 $nl
110 "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."
111 $nl
112 "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."
113 $nl
114 "For any other MIME type, the " { $link binary } " encoding is assumed, and thus the data is returned literally in a byte array." ;
115
116 ARTICLE: "http.client.errors" "HTTP client errors"
117 "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:"
118 { $subsection download-failed }
119 { $subsection too-many-redirects } ;
120
121 ARTICLE: "http.client" "HTTP client"
122 "The " { $vocab-link "http.client" } " vocabulary implements an HTTP and HTTPS client on top of " { $link "http" } "."
123 $nl
124 "For HTTPS support, you must load the " { $vocab-link "urls.secure" } " vocab first. If you don't need HTTPS support, don't load " { $vocab-link "urls.secure" } "; this will reduce the size of images generated by " { $vocab-link "tools.deploy" } "."
125 $nl
126 "There are two primary usage patterns, data retrieval with GET requests and form submission with POST requests:"
127 { $subsection "http.client.get" }
128 { $subsection "http.client.post" }
129 { $subsection "http.client.put" }
130 "Submission data for POST and PUT requests:"
131 { $subsection "http.client.post-data" }
132 "More esoteric use-cases, for example HTTP methods other than the above, are accomodated by constructing an empty request object with " { $link <request> } " and filling everything in by hand."
133 { $subsection "http.client.encoding" }
134 { $subsection "http.client.errors" }
135 { $see-also "urls" } ;
136
137 ABOUT: "http.client"