cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X

Using HTTPBuilder in custom objects on the Axeda Platform

No ratings

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:

Comments

Thanks for the example.

How to handle HTTPS sites that give "javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated" exception?

Axeda seems to use very old version of httpbuilder (0.5.0). However, latest version of httpbuilder is 0.7.2 released on May 19, 2014.

In the latest version there is at least a workaround, by using ignoreSSLIssues()

Regards

Arunkumar D

If your instance is hosted in the PTC/Axeda On Demand Center, you can send us the public TLS key of the endpoint webserver.  We can then add that to the JVM trust-store to address this issue. 

Yes, Axeda uses an old version of httpbuilder.

Version history
Last update:
‎Sep 07, 2016 11:41 AM
Updated by:
Labels (1)