Debian Codegraph @

doc/modules/guide/pages/cockpit-http.adoc

raw
1 = cockpit.js: HTTP Client
2
3 Cockpit allows access to local HTTP and REST services via this API.
4
5 [[cockpit-http-constructor]]
6 == cockpit.http()
7
8 ....
9 http = cockpit.http(endpoint, [options])
10 http = cockpit.http(options)
11 ....
12
13 Create a new HTTP client. The `+endpoint+` can be a file path starting
14 with `+/+` to connect to a unix socket, or it can be a port number to
15 connect to. The optional `+options+` argument is a javascript plain
16 object, and may include:
17
18 `+"address"+`::
19 Connect to an address other than localhost. Must be a valid host name
20 or IP address. To use this option you also must provide a port number.
21 `+"port"+`::
22 Port number to use with "address" option, when not given in
23 `+endpoint+`.
24 `+"tls"+`::
25 Object properties for an https connection. See
26 `+http-stream2 TLS options+`.
27 `+"headers"+`::
28 Additional HTTP headers to include with the HTTP request. This is a
29 plain javascript object with each key as a header name, and each value
30 as the header value.
31 `+"superuser"+`::
32 Set to `+"require"+` to open this channel as root. If the currently
33 logged in user is not permitted to become root (eg: via `+pkexec+`)
34 then the `+channel+` will immediately be
35 link:#cockpit-channels-close-ev[closed] with a `+"access-denied"+`
36 problem code.
37 +
38 Set to `+"try"+` to try to make the request as root, but if that
39 fails, fall back to perform an unprivileged request.
40 `+"tls"+`::
41 If set to a plain javascript object, then the connection will be an
42 HTTPS connection and include TLS encryption. The fields of the `+tls+`
43 object declare various TLS configuration and data. All fields are
44 optional:
45 +
46 * `+"authority"+`: Certificate authority(s) to expect as signers of
47 the server's TLS certificate, represented as a plain javascript
48 object. It should have either a `+"file"+` field containing a readable
49 PEM file on the system containing authorities, or a `+"data"+` with
50 PEM encoded certificate data.
51 * `+"certificate"+`: A client certificate to use, represented as a
52 plain javascript object. It should have either a `+"file"+` field
53 containing a readable PEM file on the system to use as a certificate,
54 or a `+"data"+` with PEM encoded certificate data.
55 * `+"key"+`: A client key to use, represented as a plain javascript
56 object. It should have either a `+"file"+` field containing a readable
57 PEM file on the system to use as a key, or a `+"data"+` with PEM
58 encoded key data.
59 * `+"validate"+`: A boolean that describes whether to validate the
60 server's TLS certificate or not. By default local connections are not
61 validated, and remote connections are validated.
62
63 Here is a somewhat complex example of using most of the above
64 `+options+` when when calling `+cockpit.http()+`:
65
66 ....
67 http = cockpit.http({
68 "address": "localhost",
69 "headers": {
70 "Authorization": "Basic dXNlcjpwYXNzd29yZA=="
71 },
72 "port": 443,
73 "tls": {
74 "validate": true,
75 "authority": {
76 "file": "/etc/pki/tls/certs/ca-bundle.crt",
77 },
78 "certificate": {
79 "data": "-----BEGIN CERTIFICATE-----\nMIIDsDCCA..."
80 },
81 "key": {
82 "data": "-----BEGIN RSA PRIVATE KEY-----\nMIIEogIBA..."
83 }
84 }
85 });
86 ....
87
88 [[cockpit-http-get]]
89 == http.get()
90
91 ....
92 request = http.get(path, [params, [headers]])
93 ....
94
95 Perform an HTTP GET request for the given `+path+`. If the `+params+` is
96 specified it should be a plain javascript object, which will be turned
97 into a query string.
98
99 Optionally a plain javascript object containing headers can be included
100 in the `+headers+` argument.
101
102 The return value is a
103 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise]
104 that will complete if the request happens successfully, or fail if
105 there's a problem.
106
107 [[cockpit-http-post]]
108 == http.post()
109
110 ....
111 request = http.post(path, body, [headers])
112 ....
113
114 Perform an HTTP POST request for the given `+path+`. The `+body+` can be
115 a string, or a javascript plain object, which will be encoded as JSON
116 data. If `+body+` is `+undefined+` or `+null+` then an empty HTTP body
117 will be sent.
118
119 Optionally a plain javascript object containing headers can be included
120 in the `+headers+` argument.
121
122 The return value is a
123 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise]
124 that will complete if the request happens successfully, or fail if
125 there's a problem.
126
127 [[cockpit-http-request]]
128 == http.request()
129
130 ....
131 request = http.request(options)
132 ....
133
134 Perform an HTTP request. The `+options+` can contain the following:
135
136 `+"body"+`::
137 The HTTP request body. If you do not specify a body, then you must
138 call link:#cockpit-http-input[request.input()] to complete the body
139 and allow the request to start.
140 `+"headers"+`::
141 A javascript plain object containing HTTP headers.
142 `+"method"+`::
143 The HTTP method. Defaults to `+"GET"+`.
144 `+"params"+`::
145 A javascript plain object containing query string parameters.
146 `+"path"+`::
147 The HTTP path. Defaults to `+/+`.
148
149 The return value is a
150 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise]
151 that will complete if the request happens successfully, or fail if
152 there's a problem.
153
154 [[cockpit-http-then]]
155 == request.then()
156
157 ....
158 request.then(data => { ... })
159 ....
160
161 This is a standard
162 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise]
163 method. It sets up a handler to be called when the request finishes
164 successfully.
165
166 The `+data+` argument contains the body result of the request. If it a
167 string, unless the process was opened in binary mode, in which case the
168 `+data+` is an array of bytes. If a `+request.stream()+` handler is set
169 up, then any standard output data consumed by the handler will not be
170 included in the `+data+` argument.
171
172 [[cockpit-http-catch]]
173 == request.catch()
174
175 ....
176 request.catch((exception[, data]) => { ... })
177 ....
178
179 This is a standard
180 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise]
181 method. It sets up a handler to be called when the request fails, or
182 returns an error code.
183
184 The `+exception+` object passed to the handler can have the following
185 fields:
186
187 `+problem+`::
188 A xref:cockpit-error.adoc#cockpit-problems[problem code] string when a problem occurred
189 starting or communicating with the server. This is `+null+` if the
190 process exited or was terminated.
191 `+status+`::
192 The numeric status of the response. This is `+null+` if no response
193 was received.
194 `+reason+`::
195 A string reason returned in the response. This is `+null+` if no
196 response was received.
197 `+message+`::
198 A string message returned in the response. This is `+null+` if no
199 response was received.
200
201 If the request returned a response body, it will be available in the
202 `+data+` argument. Otherwise this argument will be `+undefined+`.
203
204 [[cockpit-http-response]]
205 == request.response()
206
207 ....
208 request.response((status, headers) => { ... })
209 ....
210
211 This sets up a handler to be called when the HTTP request gets the
212 initial response from the server. The `+status+` argument is the HTTP
213 status integer, and the `+headers+` is a plain javascript object
214 containing the headers of the response.
215
216 [[cockpit-http-stream]]
217 == request.stream()
218
219 ....
220 request.stream(data => { ... })
221 ....
222
223 This sets up a handler to be called when the request returns output
224 data. The handler will be called multiple times.
225
226 Only one handler may be registered at a time. Registering an additional
227 handler replaces the previous one. The handler receives either string
228 `+data+` or an array of binary bytes as its argument. A stream handler
229 may return a number, which indicates the number of characters or bytes
230 consumed from `+data+`. Any data not consumed will be included again the
231 next time the handler is called.
232
233 If a `+request.stream()+` handler is set up, then the `+request.then()+`
234 handlers will only get any remaining data not consumed by the stream
235 handler.
236
237 [[cockpit-http-input]]
238 == request.input()
239
240 ....
241 request.input(data, [stream])
242 ....
243
244 This method writes `+data+` to the HTTP request body. It is only valid
245 if no `+"body"+` has been specified in
246 link:#cockpit-http-request[http.request()] options. If `+stream+` is
247 `+true+` then this function can be called again to provide further data.
248
249 [[cockpit-http-close]]
250 == request.close()
251
252 ....
253 request.close([problem])
254 ....
255
256 Cancel the request. If `+problem+` is specified it should be a standard
257 xref:cockpit-error.adoc#cockpit-problems[problem code] string.
258
259 [[cockpit-http-close-all]]
260 == http.close()
261
262 ....
263 http.close([problem])
264 ....
265
266 Cancel all outstanding requests with the given problem code. This is
267 useful when you know that the server is going down soon.