Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ut-port-http

softwaregroup-bg81Apache-2.08.9.9

UT port http module

readme

UT Port HTTP Client

Create http/s requests based on request module

Configuration

  • id: unique identification of port
  • logLevel: trace, debug or info.
  • url: remote server URL http://example.com:80.
  • uri: remote server request URI /about.html.
  • method: http request method POST or GET.
  • receive: incoming message convert function , return object or promise.
  • send: outgoing message convert function, return object or promise .
  • tls: can be used to enable TLS. For detailed information see tls.createSecureContext and request TLS/SSL Protocol

    Accepts the options:

    • cert - Path to client certificate file
    • key - Path to client certificate private key file
    • passphrase - Client certificate passphrase
    • ca - Path to trusted root certificate, usually needed for using self signed certificates.

    Additional configuration options:

  • raw: this property can be set in config file only, everything in this property will be merged with current configuration

  • parseResponse: to parse the response or not, defaults to true
  • parseOptions: to pass certain options when parsing the response (based on the content type) example:
{
    "parseResponse": true,
    "parseOptions": {
        "application/xml": {
            "explicitArray": true
        }
    }
}
  • requestTimeout: in ms, time before timeout error emit, defaults to 30 sec.
  • headers: object containing request header values.
  • namespace: Array containing different namespaces of this port.
  • start: function that will be called once on creation of port. It can be used for setting global variables and initializing objects.

Response

Response is always an object containing response from remote server or error. If server returns status code different from 200 (OK) or some error occurred during the process, the response message object will look like:

{
    $$:{
        mtid: 'error',
        errorCode: '',
        errorMessage: ''
    }
}

Message properties:

  • payload: Contains response data returned from the remote server. If header content-type contains /xml and parseResponse = true the data will be converted to javascript object, the same is for Json, if for some reason parseResponse=true and there is not parser available for parsing the response, error will be emitted
  • headers: Response headers.
  • httpStatus: Response status code.

Example

Example index.js configuration file used for making web service requests to external system. Send and receive are used for modifying message object just before it is sent and just after response is received.

var xmpParser = require('./xmlParser');
var loadTemplate;
module.exports = {
    id: 't24',
    type: 'http',
    logLevel: 'trace',
    url: 'http://twsdevcloudservice.cloudapp.net',
    uri: '/swg/swg.svc',
    method: 'post',
    namespace: ['cbs'],

    start: function() {
        loadTemplate = this.bus.importMethod('template.load');
    },

    receive:function(msg) {
        if(msg.$$.mtid == 'error'){
            return msg;
        }
        return xmpParser.parse(msg.$$.opcode, msg.payload)
            .then(function(res) {

                if(res.successIndicator != 'Success'){
                    msg.$$.mtid = 'error';
                    msg.$$.errorCode = res.messageId;
                    msg.$$.errorMessage = Array.isArray(res.message)
                        ? res.message.join('; ')
                        : res.message;
                    return msg;
                }
                msg.payload = res;
                return msg;
            });
    },

    send:function(msg) {
        msg.headers = {'Content-Type': 'text/xml'};
        var templatePath = require.resolve('./' + msg.$$.opcode + '.xml.marko');

        var template = loadTemplate(templatePath);

        return template.render(msg).then(function(res) {
            msg.payload = res;
            msg.$$.opcode = msg.opcode || msg.$$.opcode;
            return msg;
        });
    }
};

Open API

ut-port-http provides the possibility to be used as a swagger client in a semi-automatic fashion. For this aim an additional configuration property called openApi must be provided.

Example:

module.exports = (...params) => {
    return class swaggerClient extends require('ut-port-http')(...params) {
        get defaults() {
            return {
                namespace: [
                    'provider1',
                    'provider2',
                    'provider3'
                ],
                openApi: {
                    'provider1': require('./provider2-swagger.json'),
                    'provider2.segment1': require.resolve('./provider2/segment1-swagger.json'),
                    'provider2.segment2': require.resolve('./provider2/segment2-swagger.json'),
                    'provider3': 'http://www.provider3.com/swagger.json'
                }
            };
        }
    };
};

Note that the openApi configuration property represents a map where:

  • the keys determine how the operation IDs of the document will be prefixed. For example if the document is prefixed with key provider2.segment2 and contains a route with operationId operationX, then it will be accessible via the method provider2.segment2.operationX through the bus. Therefore the first parts (before the first dot) of all prefixes must be present in the namespace array.

  • the values are the documents themselves. All three approaches are acceptable as a value:

    • the content of the swagger document
    • path to a local swagger document
    • url for obtaining the document with http GET request

The message format for calling these auto-generated swagger methods has the following specifics:

  • can have a body property which will be used as a payload
  • can have a params property which ill be used for extracting path, query and header parameters.

Example:

await utMethod('provider2.segment2.operationX')({
    body: {
        payloadProp1: 'payloadValue1',
        payloadProp2: 'payloadValue2'
    },
    params: {
        param1: 'value1',
        param2: 'value2'
    }
});

For more information see ut-openapi request format

changelog

8.9.9 (2024-07-04)

8.9.8 (2023-03-30)

8.9.7 (2023-01-19)

Bug Fixes

8.9.6 (2023-01-04)

Bug Fixes

8.9.5 (2022-11-09)

Bug Fixes

8.9.4 (2022-10-01)

Bug Fixes

8.9.3 (2022-03-07)

8.9.2 (2021-11-22)

8.9.1 (2021-10-21)

8.9.0 (2021-09-24)

Features

  • enable configuration of TLS certificates (#46) (89a36d9)

8.8.0 (2021-04-01)

Features

  • enable opening from file (d29b212)

8.7.0 (2021-03-30)

Features

  • enable namespace import (4a6a463)

8.6.2 (2020-11-15)

8.6.1 (2020-11-12)

Bug Fixes

  • improve attachment handling (a1c6b44)

8.6.0 (2020-10-21)

Features

8.5.3 (2020-10-20)

8.5.2 (2020-09-08)

Bug Fixes

  • decode encoded xml body before parsing (a7a9554)
  • turn function to arrow function so that this.erros is accessible (880f1f2)

8.5.1 (2020-07-31)

8.5.0 (2020-07-06)

Features

  • support form and json passing from message (d16a550)

8.4.0 (2020-03-23)

Bug Fixes

  • make possible to pass parseOption on message level (b5f4b6a)

Features

  • pass encoding as an option when sending requests (89e159c)

8.3.0 (2020-03-19)

Features

  • pass certain parse options per content-type to be applied if parseResponse is true (4b094b9)

8.2.3 (2020-02-20)

Bug Fixes

  • modify reqProps correctly before sending the request (d2c0470)

8.2.2 (2020-02-20)

Bug Fixes

  • this.config.headers and this.config.raw (#38) (8cc43d0)

8.2.1 (2019-12-23)

Bug Fixes

  • allow lowercase http methods (6996212)

8.2.0 (2019-12-20)

Features

  • automatic methods definition based on openapi/swagger documents (d458be4)

8.1.0 (2019-12-04)

Features

8.0.5 (2019-10-03)

Bug Fixes

  • surround request callback into a try catch block to prevent process exit (#34) (a665dea)

8.0.4 (2019-10-03)

Bug Fixes

  • set request and body to empty objects when there is an error (8c25058)

8.0.3 (2019-09-18)

Bug Fixes

  • response.request is undefined when using browser-request (#32) (f149daf)

8.0.2 (2019-09-12)

Bug Fixes

  • avoid logging circular structures (7557a87)
  • update dependencies (eed0194)

8.0.1 (2019-03-22)

8.0.0 (2019-02-02)

Bug Fixes

BREAKING CHANGES

  • requires upgrade of ut-port and ut-run

7.3.0 (2018-08-02)

Bug Fixes

7.2.0 (2018-06-07)

7.1.0 (2018-03-02)

7.0.1 (2017-12-14)

Bug Fixes

7.0.0 (2017-12-06)

Bug Fixes

6.6.0 (2017-10-25)

Bug Fixes

  • handle browser case properly (0c1671b)

Features

6.5.0 (2017-07-07)

Features

6.4.9 (2017-05-22)

6.4.8 (2017-05-22)

6.4.7 (2017-05-22)

6.4.6 (2017-05-22)

6.4.5 (2017-05-22)

6.4.4 (2017-05-22)

6.4.3 (2017-05-04)

Bug Fixes

6.4.2 (2017-04-24)

6.4.1 (2017-03-08)

Bug Fixes

6.4.0 (2016-12-23)

Features

6.3.15 (2016-12-15)

Bug Fixes

6.3.14 (2016-11-25)

Bug Fixes

6.3.13 (2016-11-23)

Bug Fixes

6.3.12 (2016-11-04)

Bug Fixes

6.3.11 (2016-10-24)

6.3.10 (2016-10-12)

6.3.9 (2016-10-12)

6.3.8 (2016-10-11)

Bug Fixes

6.3.7 (2016-10-11)

Bug Fixes

  • import methods on start (a8a78a4)

6.3.6 (2016-09-26)

Bug Fixes

  • distinguish between errors and responses (2632783)

6.3.5 (2016-09-21)

6.3.4 (2016-09-21)

6.3.3 (2016-09-21)

Bug Fixes

6.3.2 (2016-09-21)

Bug Fixes

  • do not return error if status code is between success codes (2xx) (0950e1f)

6.3.1 (2016-09-20)

6.3.0 (2016-09-15)

Features

  • possibility to pass querystring object (0edc885)

6.2.0 (2016-08-26)

Features

  • allow overriding methods by full name (1b4fbb6)

6.1.4 (2016-08-12)

Bug Fixes

6.1.3 (2016-08-08)

Bug Fixes

  • do not throw the whole response as cause (9295bfc)

6.1.2 (2016-07-22)

6.1.1 (2016-07-11)

Features

6.0.3 (2016-06-16)

6.0.2 (2016-06-08)

Bug Fixes

6.0.1 (2016-05-14)

Bug Fixes

  • clean: logic cleanup, also added request headers and body to the response object (f8b144d)
  • headers: removed request headers (ae0864a)

5.3.10 (2016-04-15)

Features

  • add Gitlab-ci and Jenkins scripts (679fc38)

5.3.9 (2016-04-04)

Features

  • update ut-error dependency (424f117)

5.3.8 (2016-03-31)

Bug Fixes

Features