= cockpit.js: HTTP ClientCockpit allows access to local HTTP and REST services via this API.[[cockpit-http-constructor]]== cockpit.http()....http = cockpit.http(endpoint, [options])http = cockpit.http(options)....Create a new HTTP client. The `+endpoint+` can be a file path startingwith `+/+` to connect to a unix socket, or it can be a port number toconnect to. The optional `+options+` argument is a javascript plainobject, and may include:`+"address"+`::Connect to an address other than localhost. Must be a valid host nameor IP address. To use this option you also must provide a port number.`+"port"+`::Port number to use with "address" option, when not given in`+endpoint+`.`+"tls"+`::Object properties for an https connection. See`+http-stream2 TLS options+`.`+"headers"+`::Additional HTTP headers to include with the HTTP request. This is aplain javascript object with each key as a header name, and each valueas the header value.`+"superuser"+`::Set to `+"require"+` to open this channel as root. If the currentlylogged in user is not permitted to become root (eg: via `+pkexec+`)then the `+channel+` will immediately belink:#cockpit-channels-close-ev[closed] with a `+"access-denied"+`problem code.+Set to `+"try"+` to try to make the request as root, but if thatfails, fall back to perform an unprivileged request.`+"tls"+`::If set to a plain javascript object, then the connection will be anHTTPS connection and include TLS encryption. The fields of the `+tls+`object declare various TLS configuration and data. All fields areoptional:+* `+"authority"+`: Certificate authority(s) to expect as signers ofthe server's TLS certificate, represented as a plain javascriptobject. It should have either a `+"file"+` field containing a readablePEM file on the system containing authorities, or a `+"data"+` withPEM encoded certificate data.* `+"certificate"+`: A client certificate to use, represented as aplain javascript object. It should have either a `+"file"+` fieldcontaining a readable PEM file on the system to use as a certificate,or a `+"data"+` with PEM encoded certificate data.* `+"key"+`: A client key to use, represented as a plain javascriptobject. It should have either a `+"file"+` field containing a readablePEM file on the system to use as a key, or a `+"data"+` with PEMencoded key data.* `+"validate"+`: A boolean that describes whether to validate theserver's TLS certificate or not. By default local connections are notvalidated, and remote connections are validated.Here is a somewhat complex example of using most of the above`+options+` when when calling `+cockpit.http()+`:....http = cockpit.http({"address": "localhost","headers": {"Authorization": "Basic dXNlcjpwYXNzd29yZA=="},"port": 443,"tls": {"validate": true,"authority": {"file": "/etc/pki/tls/certs/ca-bundle.crt",},"certificate": {"data": "-----BEGIN CERTIFICATE-----\nMIIDsDCCA..."},"key": {"data": "-----BEGIN RSA PRIVATE KEY-----\nMIIEogIBA..."}}});....[[cockpit-http-get]]== http.get()....request = http.get(path, [params, [headers]])....Perform an HTTP GET request for the given `+path+`. If the `+params+` isspecified it should be a plain javascript object, which will be turnedinto a query string.Optionally a plain javascript object containing headers can be includedin the `+headers+` argument.The return value is ahttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise]that will complete if the request happens successfully, or fail ifthere's a problem.[[cockpit-http-post]]== http.post()....request = http.post(path, body, [headers])....Perform an HTTP POST request for the given `+path+`. The `+body+` can bea string, or a javascript plain object, which will be encoded as JSONdata. If `+body+` is `+undefined+` or `+null+` then an empty HTTP bodywill be sent.Optionally a plain javascript object containing headers can be includedin the `+headers+` argument.The return value is ahttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise]that will complete if the request happens successfully, or fail ifthere's a problem.[[cockpit-http-request]]== http.request()....request = http.request(options)....Perform an HTTP request. The `+options+` can contain the following:`+"body"+`::The HTTP request body. If you do not specify a body, then you mustcall link:#cockpit-http-input[request.input()] to complete the bodyand allow the request to start.`+"headers"+`::A javascript plain object containing HTTP headers.`+"method"+`::The HTTP method. Defaults to `+"GET"+`.`+"params"+`::A javascript plain object containing query string parameters.`+"path"+`::The HTTP path. Defaults to `+/+`.The return value is ahttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise]that will complete if the request happens successfully, or fail ifthere's a problem.[[cockpit-http-then]]== request.then()....request.then(data => { ... })....This is a standardhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise]method. It sets up a handler to be called when the request finishessuccessfully.The `+data+` argument contains the body result of the request. If it astring, unless the process was opened in binary mode, in which case the`+data+` is an array of bytes. If a `+request.stream()+` handler is setup, then any standard output data consumed by the handler will not beincluded in the `+data+` argument.[[cockpit-http-catch]]== request.catch()....request.catch((exception[, data]) => { ... })....This is a standardhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise]method. It sets up a handler to be called when the request fails, orreturns an error code.The `+exception+` object passed to the handler can have the followingfields:`+problem+`::A xref:cockpit-error.adoc#cockpit-problems[problem code] string when a problem occurredstarting or communicating with the server. This is `+null+` if theprocess exited or was terminated.`+status+`::The numeric status of the response. This is `+null+` if no responsewas received.`+reason+`::A string reason returned in the response. This is `+null+` if noresponse was received.`+message+`::A string message returned in the response. This is `+null+` if noresponse was received.If the request returned a response body, it will be available in the`+data+` argument. Otherwise this argument will be `+undefined+`.[[cockpit-http-response]]== request.response()....request.response((status, headers) => { ... })....This sets up a handler to be called when the HTTP request gets theinitial response from the server. The `+status+` argument is the HTTPstatus integer, and the `+headers+` is a plain javascript objectcontaining the headers of the response.[[cockpit-http-stream]]== request.stream()....request.stream(data => { ... })....This sets up a handler to be called when the request returns outputdata. The handler will be called multiple times.Only one handler may be registered at a time. Registering an additionalhandler replaces the previous one. The handler receives either string`+data+` or an array of binary bytes as its argument. A stream handlermay return a number, which indicates the number of characters or bytesconsumed from `+data+`. Any data not consumed will be included again thenext time the handler is called.If a `+request.stream()+` handler is set up, then the `+request.then()+`handlers will only get any remaining data not consumed by the streamhandler.[[cockpit-http-input]]== request.input()....request.input(data, [stream])....This method writes `+data+` to the HTTP request body. It is only validif no `+"body"+` has been specified inlink:#cockpit-http-request[http.request()] options. If `+stream+` is`+true+` then this function can be called again to provide further data.[[cockpit-http-close]]== request.close()....request.close([problem])....Cancel the request. If `+problem+` is specified it should be a standardxref:cockpit-error.adoc#cockpit-problems[problem code] string.[[cockpit-http-close-all]]== http.close()....http.close([problem])....Cancel all outstanding requests with the given problem code. This isuseful when you know that the server is going down soon.