Search

User Manual

GETTING STARTED


MODULES AND PLATFORM


APPENDIX


TUTORIALS & HELPFUL TRICKS


GLOSSARY


STRATEGIC PARTNER LINKS

Sepasoft - MES Modules
Cirrus Link - MQTT Modules

RESOURCES

Inductive University
Ignition Demo Project
Knowledge Base Articles
Forum
IA Support
SDK Documentation
SDK Examples

ALL MANUAL VERSIONS

Ignition 8.1
Ignition 7.9
Ignition 7.8

Deprecated Pages

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 21 Next »


This function is used in Python Scripting.

Description

Provides a general use object that can be used to send and receive RESTful HTTP requests. The object created by this function is a wrapper around Java's HttpClient class. Typical use involves creating the a JythonHttpClient object with a call to system.net.httpClient, and then utilizing the varous methods (get(), post(), etc) on the JythonHttpClient to handle requests.

Client Permission Restrictions

This scripting function has no Client Permission restrictions.

Syntax

system.net.httpClient()

  • Parameters

Integer timeout - A value, in milliseconds, to set the client’s connect timeout setting to. [Optional. Defaults to 60000.]

Boolean bypass_cert_validation - A value, in milliseconds, to set the client’s read timeout setting to. [Optional. Defaults to False.]

String username - A string indicating the username to use for authentication if the remote server requests authentication; specifically, by responding with a WWW-Authenticate or Proxy-Authenticate header. Only supports Basic authorization. If username is present but not password, an empty string will be used for password. [Optional. Defaults to None.]

String password - A string indicating the password to use for authentication. [Optional. Defaults to None.]

String proxy - The address of a proxy, which will be used for HTTP and HTTPS traffic. If the port is not specified, it will be assumed from the protocol in the URL, i.e. 80/443. [Optional. Defaults to None.]

String cookie_policy - A string representing this client’s cookie policy. Accepts values "ACCEPT_ALL", "ACCEPT_NONE", and "ACCEPT_ORIGINAL_SERVER" . [Defaults to "ACCEPT_ORIGINAL_SERVER"]

String redirect_policy - A string representing this client’s redirect policy. Acceptable values are listed below. [Optional. Defaults to "Normal".]

          • "NEVER" - never allow redirects
          • "ALWAYS" - allow redirects,
          • "NORMAL" - allows redirects, except those that would downgrade to insecure addresses (i.e., HTTPS redirecting to HTTP).

Callable customizer - A reference to a callable function. This function will be called with one argument (an instance of HttpClient.Builder). The function should operate on the builder class and allows for arbitrary customization of the created HTTP client. [Optional. Defaults to None.]

  • Returns

JythonHttpClient  -  An object wrapped around an instance of Java's HttpClient class. The client object has methods that can be called against a website to get or send information through the web. See the panel below for more details.

  • Scope

Gateway, Vision Client, Perspective Session

JythonHttpClient

Once a JythonHttpClient object has been created, HTTP calls can be made with the build in methods detailed below. The various methods available on this object can be split under different categories.

JythonHttpClient Methods

 Methods

This section details synchronous methods. Each method in this section returns a Response object.

Methods

The following methods return a Response object. Parameters for these functions are documented below.

MethodDescriptionReturn type
.get()
Sends an HTTP GET call, blocking for a response.Response
.getAsync()
Sends an HTTP GET call without blocking.Promise
.post()
Sends an HTTP POST call, blocking for a response.Response
.postAsync()
Sends an HTTP POST call without blocking. Promise
.put()
Sends an HTTP PUT call, blocking for a response.Response
.putAsync()
Sends an HTTP PUT call without blocking. Promise
.delete()
Sends an HTTP DELETE call, blocking for a responseResponse
.deleteAsync()
Sends an HTTP DELETE call without blocking. Promise
.patch()
Sends an HTTP PATCH call, blocking for a response.Response
.patchAsync()
Sends an HTTP PATCH call without blocking. Promise
.head()
Sends an HTTP HEAD call, blocking for a response.Response
.headAsync()
Sends an HTTP HEAD call without blocking. Promise
.options()
Sends a blocking HTTP OPTIONS request.Response
.optionsAsync()
Sends a non-blocking HTTP OPTIONS request.Promise
.trace()
Sends an HTTP TRACE call, blocking for a response.Response
.traceAsync()
Sends an HTTP TRACE call, without blocking for a response. Promise
.request()
Sends an HTTP request, with the verb being dictated by the method parameter. Use this method in cases where non-standard verb is required, and you need the call to block.Response
.requestAsync()
Sends an HTTP request, with the verb being dictated by the method parameter. Use this method in cases where non-standard verb is required, and you do not want the request to block. Promise
.getJavaClient()
Returns the underlying Java HTTPClient.HTTPClient
.getCookieManager()
Returns a CookieManager, which can be used to override the cookie storage policy on the JythonHttpClient.CookieManager


Parameters

Parameters in this section can be used by any of the methods above. Exceptions to this rule will be defined on each parameter.

String url - The URL to connect to. [Required]

String method - The method to use in the request. [Required - used by .request() and .requestAsync() only]

String or Dictionary params - Parameters to append to the URL. [Optional. Defaults to None.]

        • If supplied as a string, will be directly appended to the URL.
        • If supplied as a dictionary, key/value pairs will be automatically URL encoded.

String or Dictionary or byte[] - Data to send in the request. [Optional - Defaults to None]

        • String data will be sent with a Context-Type of "text/plain; charset=UTF-8", unless the Content-Type header was already specified
        • Dictionary data will be automatically encoded into JSON. The Content-Type header will be set to "application/json;charset=UTF-8" unless the Content-Type header was already specified.
        • Byte arrays will be sent directly, and set the Content-Type header to application/octet-stream unless the Content-Type header was already specified.

String file - The path to a file relative to the HTTP client. If specified, and the path is valid, will directly send the data in the file. The file attribute overrides any value set in data; only the file’s data will be sent. [Optional. Defaults to None.]

Dictionary headers - A dictionary of headers to add to the request. [Optional. Defaults to None.]

String username - Username to add to a Basic Authorization header in the outgoing request. If username is specified, but not password, password is assumed to be an empty string. [Optional. Defaults to None.]

String password - Password to add to a Basic Authorization header in the outgoing request. [Optional. Defaults to None.]

Integer timeout - Timeout for this request, in milliseconds. [Optional. Defaults to 60000.]

JythonHttpClient Attributes

 Click here to expand...

This section documents available attributes on the JythonHttpClient object, as well as getter methods for those attributes.

AttributeDescriptionReturn Type
.javaClient

Returns the underlying Java HTTPClient.

HTTPClient
.cookieManager

Returns a CookieManager, which can be used to override the cookie storage policy on the JythonHttpClient.

Setting the cookie policy is easiest on the initial system.net.httpClient call, but the policy on the CookieManager can be overridden with a call to the built-in setCookiePolicy method. Policies are defined under the Java CookiePolicy interface. A usage example is demonstrated below.

CookieManager
CookieManager

Once a JythonHttpClient object has been created, HTTP calls can be made with the build in methods detailed below. The various methods available on this object can be split under different categories.

CookieManager Methods

 Methods

This section details methods on the CookieManager. Setting the cookie policy is easiest on the initial system.net.httpClient call, but the policy on the CookieManager can be overridden with a call to the built-in setCookiePolicy method. Policies are defined under the Java CookiePolicy interface.

Methods

MethodDescriptionReturn type
.getJavaClient()
Returns the underlying Java HTTPClient.HTTPClient
.getCookieManager()
Sends an HTTP GET call, blocking for a response.CookiePolicy
.setCookiePolicy(policy)
Sets the CookiePolicy. See the Java CookiePolicy interface for more information.None
from java.net import CookiePolicy

client = system.net.httpClient()
manager = client.getCookieManager()
manager.setCookiePolicy(CookiePolicy.ACCEPT_NONE)
Response Object

This section documents the Response object, generally returned by the blocking methods returned by the JythonHttpClient object. This object is simply a wrapper for Java's HTTPResponse object.

Response Methods

 Methods

This section details methods on the Response object.

Methods and Attributes

Method and/or AttributeDescriptionData type
.getBody()
.body
Returns the response content directly.Byte Array
.getJson([encoding])
.json
Return the response content as a dictionary, with the encoding specified by the response. The optional encoding parameter can be used to specified how the JSON should be decoded into Python (dictionary, list, etc). If response can’t be decoded to JSON, the method will throw an error.Dictionary
.getText([encoding])
.text
Return the response content decoded into a string - either with the charset specified in the response (or UTF-8 if not specified), or using the encoding specified in the function call.String
.getStatusCode()
.statusCode
Return the status code of the response object (i.e., 200 or 404).Integer
.isGood()
.good
Returns True if the response was good (i.e., 200) or False if it was a client or server error (status code between 400 and 599).Boolean
.isClientError()
.clientError
Returns True if the response was a client error, as in an HTTP 4XX response.Boolean
.isServerError()
.serverError
Returns True if the response was a server error, as in an HTTP 5XX response.Boolean
.getUrl()
.url
Returns the URL this Response connected to.String
.getHeaders()
.headers
Returns a case-insensitive “dictionary” of headers present on the response. Values will be in a list, even if only a single value is present.Dictionary
.getJavaResponse()
.javaResponse
Returns the underlying Java HttpResponse behind this Response.HttpResponse
getCookieManager()
.cookieManager
Returns the CookieManager. See the CookieManager section for more details.CookieManager
Promise Object

This section documents the Promise object, generally returned by the non-blocking methods returned by the JythonHttpClient object. This object is simply a wrapper for Java's CompletableFuture object that returns some type <T>.

Promise Methods

 Methods

This section details methods on the Response object.

Methods

The following methods return a Response object.

Method and/or AttributeDescriptionData type
.get([timeout])
Block for timeout until a result is available. The result object can technically be any type, if chaining, but will be a Response object if calling one of the HTTPClient methods directly. If the timeout is met without a result, throws an exception. The default timeout is 60 seconds.<T>
.then(callback)
Allows chaining, returning a new Promise, wrapping callback. The callback parameter should be a python function that either accepts two arguments (result, error) or a single argument able to accept exceptions as well as valid values.Promise
.handleException(callback)

In the event of an exception in a potential chain of promises, handleException will be called with one argument (the thrown error) and is expected to return a new fallback value for the next step in the promise chain.

Promise
.whenComplete(callback)
Call the provided callback when this promise finishes evaluating. Callback will be called with return value as the first argument, and any thrown error as the second argument. Any return value will be ignored.None
.cancel()
Attempt to cancel the wrapped Java future. Returns True if the cancellation succeeded.Boolean
.getFuture()
.future
Returns the underlying Java CompletableFuture object that this Promise contains.CompletableFuture
.isDone()
.done
Returns True if the underlying future has completed - with a good result, or an exception.Boolean
RequestWrapper Object

This section documents the RequestWrapper object, which is simply a wrapper around Java's HTTPRequest object.

RequestWrapper Methods

 Methods

This section details methods on the Response object.

Methods

The following methods return a Response object.

Method and/or AttributeDescriptionData type
.getUrl()
.url
Returns the actual URL that was contacted in the request.String
.getMethod()
.method
Return the HTTP method used in this request; GET, POST, PATCH, etc.String
.getHeaders()
.headers
Returns a case-insensitive “dictionary” of headers present on the request. Values will always be in a list, even if only a single value is present.Dictionary
.getTimeout()
.timeout
Returns the timeout this query was set to, or -1 if timeout was invalid.Integer
.getVersion()
.version
Returns the HTTP version used for this request; either HTTP_1_1 or HTTP_2.String
.getJavaRequest()
.javaRequest
Returns the underlying HttpRequest object directly.HttpRequest

Code Examples
# Create the JythonHttpClient
client = system.net.httpClient()

# Sent a GET request
response = client.get("https://httpbin.org/get", params={"a": 1, "b": 2})

# Validate the response
if response.good:
	# Do something with the response
    print response.json['args']['a']
Example - Waiting for a Response
client = system.net.httpClient()


# Send a non-blocking request to an endpoint that will wait 3 seconds
promise = client.getAsync("https://httpbin.org/delay/3", params={"a": 1, "b": 2})


# This will print before we get a response from the endpoint.
print "doing something while waiting..."
# do more work here...


# After the work on the previous lines, we can now block and wait for a response
response = promise.get()
if response.good:
    print response.json['args']['a']
  • No labels