Web Services in your own language: Part 3 – XMLHTTP

sugarcrmdevelopers —  November 23, 2010 — Leave a comment

Editor’s Note: This is the third installment in a series where we look at calling the Sugar Web Services API in various languages. Today we’ll see how to connecting using the Microsoft XMLHTTP library, which is a good solution that will work with any language being used on the Windows platform that supports calling COM objects. Next time, we’ll see an example of connecting using the more modern .NET platform

XMLHTTP is an ages-old technology in the Windows world, being first developed by Microsoft for Internet Explorer 5 for use with the Outlook Web Access client. It was the basis for AJAX, which brought about Web 2.0 and more feature-rich and interactive web applications. It is exposed as a native COM object, available on any version of Windows with IE 5 or higher, and thus can be used with any programming language on Windows that supports working with COM objects. It becomes very handy for those languages that don’t have their own native ways to make HTTP calls, such as VBScript, JScript, and Visual Foxpro ( which is the language I spent a lot of time with in a previous role, and became quite familiar with this library ).

Today, I’ll show a simple example of how to connect to SugarCRM web services using REST and VBScript. To begin with, you’ll need to instantiate the XMLHTTP request object.

Option Explicit

Dim restReq, url, userName, passwordAsMD5, data, subdata

Set restReq = CreateObject('Microsoft.XMLHTTP')

Now we will make the login request to the server. Unfortunately, VBScript has no JSON handling, so we will need to build the JSON request by hand.

url = 'http://localhost/sugarcrm/service/v2/rest.php'
userName = 'admin'
passwordAsMD5 = 'ada15bd1a5ddf0b790ae1dcfd05a1e70'

data = '{''user_auth'':{''user_name'':''' + userName + ''',''password'':''' + passwordAsMD5 + '''}}'

restReq.open 'POST', url, false
restReq.setRequestHeader 'Content-Type', 'application/x-www-form-urlencoded'
restReq.send 'method=login&input_type=JSON&response_type=JSON&rest_data=' + data

WScript.echo restReq.responseText

The above script outputs the following:

C:UsersJohn Mertic>cscript SugarWebServicesTest.vbs
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

{'id':'cpnand1758hcar7112bodafmm5','module_name':'Users','name_value_list':{'user_id':{'name':'user_id','value':'1'},'user_name':{'name':'user_name','value':'admin'},'user_language':{'name':'user_language','value':'en_us'},'user_currency_id':{'name':'user_currency_id','value':'-99'},'user_currency_name':{'name':'user_currency_name','value':'US Dollars'}}}

While VBScript is not the best choice of development platforms out there, this same example should apply just as well to any language that can call Windows COM objects such as JScript, classic Visual Basic, or Visual Foxpro. In the next installment of the series, we’ll look at a more modern approach to web services using the .NET framework and C#.

No Comments

Be the first to start the conversation!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s