One of the recurring patterns on the Axeda Platform is making requests from custom objects to other services, to be called either via Scripto, or through Expression Rules that help integrate Axeda data with your custom systems or third parties such as Salesforce.com.  Java developers would normally use a URLConnection to do this, but due to security requirements, access to the URLConnection API is sandboxed, and the HTTPBuilder API is provided instead.


Below is a short example of GETting a payload from http://www.mocky.io/v2/57d02c05100000c201208cb5 to your custom object.  One of the requirements of many services is being able to pass in API keys as part of the header request.  While in this example the API key is embedded in the code, the recommended way of storing API keys on the Axeda Platform is to use the External Credential lockbox API.  This allows you to change the API keys securely without needing to change code.

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*

def http = new HTTPBuilder('https://www.mocky.io')

http.request( GET, JSON ) {
    uri.path = '/v2/57d02c05100000c201208cb5'
    uri.headers.'appKey' = '7661392f-2372-4cba-a921-f1263c938090' 

    response.success = { resp ->
        println "POST response status: ${resp.statusLine}"
        logger.info "POST RESPONSE status: ${resp.statusLine}" 

        assert resp.statusLine.statusCode == 201
    }
}

 

An example for Salesforce might look like so:

 

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*

def xml_body = """<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
  <env:Body>
    <n1:login xmlns:n1="urn:partner.soap.sforce.com">
      <n1:username>johndoe@example.com</n1:username>
      <n1:password>Password+SECRETKEY</n1:password>
    </n1:login>
  </env:Body>
</env:Envelope>
"""

def http = new HTTPBuilder('https://login.salesforce.com/')
http.request( POST ) {
    uri.path = '/services/Soap/u/35.0 '
    body = xml_body

    response.success = { resp ->
        println "POST response status: ${resp.statusLine
        logger.info "POST RESPONSE status: ${resp.statusLine}" 

        assert resp.statusLine.statusCode == 201
    }
}

 

This request will give you a security token you can use in future calls to Salesforce APIs; you would use Groovy's native XmlSlurper/XmlParser to parse the response and get the session id to use in future requests.  You would then use this session id like in the following example to get the available REST resources:


import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*

def http = new HTTPBuilder('https://na1.salesforce.com/')

http.request( POST ) {
    uri.path = '/services/data/v29.0'
    uri.headers.'Authorization' = 'Bearer SESSIONID'

    response.success = { resp ->
        println "POST response status: ${resp.statusLine}"
        logger.info "POST RESPONSE status: ${resp.statusLine}" 

        assert resp.statusLine.statusCode == 201
    }
}


Further reading: