For up-to-date product documentation, see the IBM MobileFirst Foundation Developer Center.


Implementing JavaScript HTTP adapters

Learn to develop a JavaScript HTTP adapter.


Before you begin

The example here shows how to implement an adapter that connects with back-end HTTP services by using the connectivity facilities that are provided with MobileFirst Server. We can learn more about connectivity in The JavaScript adapter framework and HTTP adapter connectionPolicy element.

HTTP adapters work with RESTful and SOAP-based services, and can read structured HTTP sources such as RSS feeds.

We can easily customize HTTP adapters with simple server-side JavaScript code. For example, we can set up server-side filtering if necessary. The retrieved data can be in XML, HTML, JSON, or plain text format.

There are three parts to the implementation of a JavaScript adapter:

This page also shows you how to call a SOAP-based service in the HTTP adapter.

Parent topic: Developing JavaScript adapter code


Configuring the adapter.xml descriptor file


Procedure

  1. In the adapter-descriptor file, configure the following parameters inside the connectivityelement:

    • Set the protocol to http or https.
    • Set the HTTP domain to the domain part of the HTTP URL.
    • Set the TCP Port.

    <?xml version="1.0" encoding="UTF-8" standalone="no"?> <mfp:adapter name="JavaScriptHTTP" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mfp="http://www.ibm.com/mfp/integration" xmlns:http="http://www.ibm.com/mfp/integration/http"> <displayName>JavaScriptHTTP</displayName> <description>JavaScriptHTTP</description> <connectivity> <connectionPolicy xsi:type="http:HTTPConnectionPolicyType"> <protocol>https</protocol> <domain>mobilefirstplatform.ibmcloud.com</domain> <port>443</port> <connectionTimeoutInMilliseconds>30000</connectionTimeoutInMilliseconds> <socketTimeoutInMilliseconds>30000</socketTimeoutInMilliseconds> <maxConcurrentConnectionsPerNode>50</maxConcurrentConnectionsPerNode> </connectionPolicy> </connectivity> </mfp:adapter>

  2. Declare a getFeed procedure to get an RSS feed and a getFeedFiltered procedure to specify XSL transformation options on the feed data.

    Note: The name of the procedure that you declare in the adapter-descriptor file must be identical to the name we use when you implement the procedure itself.

    <procedure name="getFeed"/> <procedure name="getFeedFiltered"/>


JavaScript procedure implementation


Before you begin

A service URL is used for procedure invocations. Some parts of the URL are constant. For example, http://example.com/. Other parts of the URL can be parameterized, that is, substituted at run time by parameter values that are provided to the MobileFirst procedure. The following URL parts can be parameterized:

For more information about advanced adapter options, such as cookies, headers, and encoding, see HTTP adapter connectionPolicy element.


Procedure

Call an HTTP request by using the MFP.Server.invokeHttp function. Note that MFP.Server.invokeHttp requires an input parameter object, which must specify the following options:

For a complete list of options, see the MFP.Server.invokeHttp function.

function getFeed() { var input = { method : 'get', returnedContentType : 'xml', path : "feed.xml" }; return MFP.Server.invokeHttp(input); }


XSL transformation filtering


Before you begin

We can also apply XSL transformation to the received data. For example, to filter the feed data.


Procedure

  1. Create a filtered.xsl file in the same location as the JavaScript implementation file.
  2. Specify the transformation options in the input parameters of the getFeedFiltered procedure invocation.

    function getFeedFiltered() { var input = { method : 'get', returnedContentType : 'xml', path : "feed.xml", transformation : { type : 'xslFile', xslFile : 'filtered.xsl' } }; return MFP.Server.invokeHttp(input); }


Creating a SOAP-based service request


Before you begin

We can use the MFP.Server.invokeHttp function to create a SOAP envelope.


Procedure

  1. To call a SOAP-based service in a JavaScript HTTP adapter, encode the SOAP XML envelope within the request body by using ECMAScript for XML (E4X).

    var request = <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <GetCitiesByCountry xmlns="http://www.webserviceX.NET"> <CountryName>{countryName}</CountryName> </GetCitiesByCountry> </soap:Body> </soap:Envelope>;

  2. Use the MFP.Server.invokeHttp (options) function to call a request for a SOAP service. The options argument is a JSON object that must include the following properties:

    • A method property: usually POST
    • A returnedContentType property: usually XML
    • A path property: a service path
    • A body property: content (SOAP XML as a string) and contentType

    var input = { method: 'post', returnedContentType: 'xml', path: '/globalweather.asmx', body: { content: request.toString(), contentType: 'text/xml; charset=utf-8' } }; var result = MFP.Server.invokeHttp(input);