Creating web services

Without authentication

In general a web service takes in an HTTP request, processes it and sends a response.

Steps

  1. Take in an HTTP request using a From HTTPS channel component
  2. Parse the request into a Lua table using net.http.parseRequest{}
  3. Process the parsed request
  4. Optional: Take some action based on the processing
  5. Send a response using net.http.respond{}

Example

Try it now: Hello World, no authorization.

This web service is very simple! We just respond to an HTTP request by returning “Hello World” as XML. It’s implemented using a single channel set up with From HTTPS –> To Channel. We just take in an HTTP request, push the message to the queue, and return “Hello World” as XML.

To run the example

  1. Create a From HTTPS –> To Channel channel
  2. Set the Source > Use Translator to “Translator”
  3. Then set the “Source>URL path” to “hello”, and note the web link that is created
    Screen Shot 2014-03-11 at 13.34.30
    Note: The port number will be different from that used for Iguana:
  4. Copy the code snippet into the From HTTPS script
  5. Use the URL from step 3 to access the web service

Note: The key value entity_type=’text/xml’ tells the receiver that the response is XML, rather than the default HTML (to prevent browsers from showing a blank screen).

function main(Data)

  -- parse the request data
  local R = net.http.parseRequest{data=Data}

   -- add data processing here

   -- push the result to the queue
   queue.push{data=Data}

   -- vary response based on processing
   net.http.respond{
      entity_type='text/xml',
      body="<text>Hello World</text>", debug_result=true}
end
Tagged:

One Comment

  1. Pingback: Auto-Generating Code for Easy Reporting and Alerts

Leave A Comment?