Connecting to web services

Without authentication”

In general when connecting to web services, you use the HTTP API to pull or push the data. The XML or JSON API can be very useful to parse or generate data.

Steps

To Pull data from a web service

  1. Pull data using net.http.get{} or net.http.post{}
  2. Parse the data
  3. Process the data
  4. Forward the data to a database, file, web server, LLP, onto the queue, etc

To Push data to a web service

  1. Generate the data
  2. Push the data using net.http.put{}

Example

Try the Hello World web service, no authorization is needed.

In this case we get the data with net.http.get{}. Then we parse it with xml.parse{}, and finally we push the parsed data onto the queue for later processing.

To run the example:

  1. Create a From Translator –> To Channel channel
  2. Copy the code snippet into the From Translator script
URL = 'http://example.interfaceware.com:6544/hello'

function main()
   -- get data from web service
   local R = net.http.get{url=URL, live=true}

   -- parse and extract data
   local X = xml.parse{data=R}

   -- add data processing here

   -- push the result to the queue
   queue.push(X.text[1]:nodeValue())
end

Using basic authentication [top]

When connecting to authenticated web services, you use the HTTP API to pull or push the data, and the auth parameter to supply the login information. The XML or JSON API can be very useful to parse or generate data.

Steps

To Pull data from a web service

  1. Pull data using net.http.get{} or net.http.post{}
    • Pass the login information using the auth parameter
  2. Parse the data
  3. Process the data
  4. Forward the data to a database, file, web server, LLP, onto the queue, etc

To Push data to a web service

  1. Generate the data
  2. Push the data using net.http.put{}
    • Pass the login information using the auth parameter

Example

Try the Get current date and time web service, username = admin and password = password.

In this case we get the data with net.http.get{} using the auth parameter to pass the login details. Then we parse it with xml.parse{}, and finally we push the parsed data onto the queue for later processing.

To run the example:

  1. Create a From Translator –> To Channel channel
  2. Copy the code snippet into the From Translator script
require("node")

URL = 'http://example.interfaceware.com:6544/current_time'
USER = 'admin'
PASSWORD = 'password'

function main()
   -- get data from web service
   local R = net.http.get{url=URL,
      auth={username=USER, password=PASSWORD}, 
      live=true}

   -- parse and extract data
   local X = xml.parse{data=R}

   -- add data processing here

   -- push the result to the queue
   queue.push(X:S())
end

Leave A Comment?