Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Groovy Goodness: Access XML-RPC API

DZone's Guide to

Groovy Goodness: Access XML-RPC API

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Recently I had to access the XML-RPC WordPress API for a small project. Luckily with Groovy we can access a XML-RPC server in a Groovy way. We need to use the Groovy XML-RPC module, which is a separate dependency. The module provides code to write a XML-RPC server, but also code to access a XML-RPC server as client. We use the class XMLRPCServerProxy in the package groovy.net.xmlrpc to act as a client to a XML-RPC API. The XMLRPCServerProxy class only needs to know the URL to access the API. All API methods are dynamically dispatched to the server, so it is very flexible.

In the following Groovy script we use the WordPress XML-RPC API to access posts:

@Grab('org.codehaus.groovy:groovy-xmlrpc:0.8')
import groovy.net.xmlrpc.*

// Use correct blog identifier.
def blogId = 0 

// Use correct URL for XML-RPC API.
def blogUrl = 'http://blog/xmlrpc' 

def blogUsername = 'mrhaki'
def blogPassword = 'secret'

// Create client access for XML-RPC API.
// The second parameter is set to true
// to autodetect encoding. Default encoding
// is ISO-8859-1.
def wordPress = new XMLRPCServerProxy(blogUrl, true)

// Now we can access the API methods from WordPress.
// Notice the API method wp.getPosts is dynamically
// available from the XMLRPCServerProxy instance.
def latestFivePost = 
    wordPress
        ."wp.getPosts"(
             blogId,
             blogUsername,
             blogPassword,       
             [number: 5, post_type: 'post'],
             ['post_id', 'post_title'])

latestFivePosts.each { post ->
    println "Blog post (#${post.post_id}): ${post.post_title}"
}

Written with Groovy 2.4.


Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}