HttpClient

Description

The HttpClient object provides methods for sending HTTP request to a HTTP server. All common methods (GET, PUT, POST, DELETE) are supported. Unlike HttpRequest, this object is a singleton and has no properties. Instead all parameters have to be passed to the corresponding methods explicitly. Responses/errors have to be processed by callbacks supplied as the last parameters.

This object was introduced in InCore 2.9.

› Inherits:

Object

Overview

Properties

Methods

Signals

Properties

Methods

del(String url, List headers, JSValue responseCallback, JSValue errorCallback)

This method sends a HTTP request with the DELETE method to the server. It’s mainly used for deleting resources at the given URL.

See RFC2616 Section 9.7 for details.

get(String url, List headers, JSValue responseCallback, JSValue errorCallback)

This method sends a HTTP request with the GET method to the server. It’s mainly used for downloading resources at the given URL.

See RFC2616 Section 9.3 for details.

post(String url, List headers, String data, JSValue responseCallback, JSValue errorCallback)

This method sends a HTTP request with the POST method to the server. It’s mainly used for annotation of existing resources, providing a block of data, such as the result of submitting a form, to a data-handling process or extending a database through an append operation.

See RFC2616 Section 9.5 for details.

put(String url, List headers, String data, JSValue responseCallback, JSValue errorCallback)

This method sends a HTTP request with the PUT method to the server. It’s mainly used for storing resources under the given URL.

See RFC2616 Section 9.6 for details.

Example

import InCore.Foundation 2.9
import InCore.Http 2.9

Application {
    onCompleted: {
        HttpClient.get("https://www.inhub.de/", [[HttpHeader.UserAgent, "InCore"], ["Session", "123"]], (response) => {
                           if (response.statusCode === HttpResponse.OK)
                           {
                               console.log("Received", response.content.data.length, "bytes")
                           }
                           else
                           {
                               console.log("HTTP status code received:", response.statusCode)
                           }
                       });

        HttpClient.get("https://zzz.inhub.de", [], undefined, (error) => {
                           if (error === HttpRequest.HostNotFoundError)
                           {
                               console.log("Host not found")
                           }
                           else
                           {
                               console.log("HttpClient error:", error)
                           }
                       });

        HttpClient.post("https://httpbin.org/post", [], "InCore.Http POST Example",
                        (response) => console.log("Response for POST:", response.statusCode, response.content.data["data"]));

        HttpClient.put("https://httpbin.org/put", [], "InCore.Http PUT Example",
                       (response) => console.log("Response for PUT:", response.statusCode, response.content.data["data"]));
    }
}