07. Connecting to Cloud Services
In an earlier section, we used the Web Request 2 Node to interact with the employee web service. While this is an example of a cloud service, we had to manually figure out the URL and parse the response coming back.
In most real world scenarios, we want the connector to help us by providing a list of supported calls (interactions) and allowing us to see schemas or samples request and response payloads.
Both of these features are provided when we interact with the two most popular standards for web services - REST and SOAP. In this section, we're going to see how to use the REST Request and SOAP Request 2 Connectors for each of these respectively but before we get to that, it will be helpful to have a high level understanding of the HTTP protocol that underpins both REST and SOAP.
HTTP
HTTP (Hypertext Transfer Protocol), is the fundamental protocol used on the internet. It defines the rules for how data is exchanged between a client (for example a browser or other app) and a server.
When you type a URL into your browser or click a link, your device sends an HTTP request to the server asking for a specific webpage or resource. The server then responds with an HTTP response, delivering the data (such as the text, images, and layout) that makes up the webpage, which your browser displays.
Below is a simplified example of an HTTP request for google.com. In other words, this is what your browser will send when you request that webpage.
GET /complete/search?... HTTP/1.1
Host: www.google.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)...
...
Connection: keep-alive
GETis an HTTP verb - it shows the action that is performed, in this case we are retrieving an existing resource. Here are some common HTTP verbs:GETretrieves an existing resourcePOSTgenerally creates a new resourcePUTgenerally updates an existing resourceDELETEdeletes a resource
/complete/search?...is the location of the resource at theHostspecified. In other words, the full URL ishttps://www.google.com/complete/search?...Hostis an example of a request header. The value for theHostheader iswww.google.com. This tells the server that the request is intended for the domainwww.google.com.
When a request like the one above is received, the server will send a response like this one:
HTTP/1.1 200 OK
Date: Wed, 06 Nov 2024 12:00:00 GMT
Content-Type: text/html; charset=UTF-8
...
Connection: keep-alive
<html>
...
200 OKis the HTTP response code and description indicating that the request was successfully fulfilled. Here are some common HTTP status codes:200 OKindicates success404 Not Foundindicates that the resource being referenced does not exist401 Unauthorizedindicates that the consumer is not authorized to perform the request500 Internal Server Errorindicates a problem occurred while processing the request
Content-Typeis an example of a response header. This header is telling us the type of content that follows in the body.<html>is the start of the response body. In this case, based on the content type oftext/html, this is an HTML document but it could also have been a binary document like a PDF or ZIP file.
REST services
REST (REpresentational State Transfer) is a modern type of API that will generally be available for apps and services that were "born in the cloud". REST's philosophy is to align closely with HTTP by reusing it's concepts. Specifically, the HTTP verb indicates the action to take while the URL identifies the location of the resource the action should be taken against.
Here are some examples of the way a REST API for an eCommerce platform might work.
Retrieve a list of orders after a certain date:
GET /orders?startDate=2020-01-01
Retrieve the details of a specific order:
GET /orders/10023
Create a new order:
POST /orders
Host: api.someclouderp.com
Content-Type: application/json
...
{
"customerId": 123,
"orderId": "456",
"lines": [
{
"stockCode": "abc",
"quantity": 1
}
]
}
In Flowgear, you'll use the REST Request Connector to interact with a REST service. The Connector will handle authentication for you and also make samples available of the different interactions that are supported. This is made possible by a standard called OpenAPI (formerly Swagger) which is a document that describes all of the supported operations along with their request and response payloads.
Exercise 08: Pet Store
In this exercise, we'll connect to a demo pet store service, then we'll add a pet, query a pet by its id and finally get a list of all available pets.
In a new Workflow, add
REST Requestand rename it toadd petCreate a new
Connectiononadd petand then edit it (click✎).In the Connection Pane, set
OpenApiUrltohttps://petstore.swagger.io/v2/swagger.json.https://petstore.swagger.io/v2/swagger.jsonis the location of the OpenAPI (Swagger) document that describes the service we'll connect to - you can open this URL in a new browser tab to see what this document looks like.When you're connecting to a REST service for the first time, you need to establish the URL for the OpenAPI definition of the service. This will generally be prominently shown in the help center of the third party app or service.
Set
Urltohttps://petstore.swagger.io/v2.This is the base URL of the actual REST service and should also be prominently announced in the third party app or service help center.
Click
Refresh Metadata(you may need to scroll down to see this button).Refresh Metadataqueries the service information at the location provided inOpenApiUrland stores a copy of it within Flowgear.Save the Connection, but note that testing it is not required, as it will return a "(404) Not Found" Error. After saving, return to the Workflow.
On
REST Request, clickv → Choose Sampleand choose theaddPetsample.The Node Properties will be updated to configure the Node for an
addPetcall.Open the value for
Requestto see the structure of the document that is expected. Remove all fields exceptname,tagsandstatusin order to create a simpler request like this one:{ "name": "dog", "tags": [ { "name": "large breed" } ], "status": "available" }Add another
REST Requestand rename it toget pet.Set
get pet.Connectionto the same Connection you have assigned onadd pet.Connection.Select the sample
getPetByIdonget pet.Connect
add pet.Response → get pet.petId, the mapping expression on this Flow Connector to theidProperty (clickfxon the Flow Connector and pick theidProperty from the list).Connect
Start.RunNow → add petandadd pet → get pet.Run the Workflow to confirm that the detail of the pet created in
add petare being returned inget pet.Response.For one last variation, we'll now filter for all pets with a specific tag.
Add another
REST Requestand rename tofilter pets.Assign the same Connection as the other two
REST Request Nodestofilter pets.Select the sample
findPetsByTagsonfilter pets.Set
filter pets.tagsto `large breed.Connect
get pet → filter pets.Check that
filter pets.Responseis showing the large breed pets you've just added.
Save and run your Workflow, then click Submit Exercise to grade it.
SOAP Services
The other type of web service you'll frequently need to connect to, especially in enterprise environments, is a SOAP (Simple Object Access Protocol) service.
While SOAP services usually run on top of HTTP, they aren't as integrated into it as REST services.
Just as an OpenAPI document describes a REST service, a WSDL (Web Service Definition Language) document describes a SOAP service. In fact, one advantage of a SOAP service is that the location of the WSDL can normally be inferred automatically as there is a higher level of standardization than in the case of REST.
Exercise 09: Calculator
In this exercise, we'll connect to a calculator SOAP service.
Add
SOAP Request 2to a new Workflow, rename it tocalculatorCreate a new Connection on
calculator, then edit it (click✎).In the Connection, set
Urltohttps://zorkco.flowgear.net/calculator?auth-key=lCQmYr-Fww9UJncjS8k7wCLEVqUQrm__hnsp9X7qrpQyY2lMGChPX8bzTA-JmQkTmDhuKUgnrhdNbwRmIWRBHA.Click
Refresh Metadata(you may need to scroll down to see this button).Refresh Metadataqueries the service information and stores a copy of it within Flowgear. If theWsdlUrlProperty is left empty, the Connector will attempt to infer the location of the WSDL.Save and test the Connection, then return to the Workflow.
On
calculatorclickv → Choose Sampleand choose theAddsample.Add
Variable Barand rename toinputs.Add
inputs.number1andinputs.number2.Set
inputs.number1to40andinputs.number2to2.Add Custom Properties
calculator.number1andcalculator.number2.Add Flow Connectors
inputs.number1 → calculator.number1andinputs.number2 → calculator.number2.Edit
calculator.RequestXmlso that it references the Custom Properties. It should look like this:<Request> <intA>{number1}</intA> <intB>{number2}</intB> </Request>Add
Variable Barto the right ofcalculator, rename it tooutputs.Add
outputs.result.Add
calculator.ResponseXml → outputs.result, the mapping expression on this Flow Connector toResponse(clickfxon the Flow Connector and pick theResponseProperty from the list).Add
Start.RunNow → calculator.Run the Workflow and you should see the value
42being returned in theresultProperty of theStartWorkflow Log.
Save and run your Workflow, then click Submit Exercise to grade it.