The Rest webservice OAuth2.0 is a way to communicate with InterFormNG2, by sending normal http/https requests. The requests are authorized by using OAuth2.0.
The REST webservice basic is easier to get started with, but OAuth2.0 is recommended.
The parameters in the workflow component are:
And the parameters:
username: which is username/tenantId – example default/home
password: example: password
grant_type: password
The access token is returned in a json reply.
See the method “Login” in the java source below.
If you use https for the connection, then you have 3 options:
Notes: The webservice will return code 200, if the URL exists (404 if it does not), even if an error has occurred.
When the workflow is done the current payload of the workflow is returned to the web service caller. This can e.g. be a PDF file, that has been created with the workflow component, Create PDF document or it can e.g. be an XML file which indicates if the workflow was successful or not.
Relevant functions are:
If you change the payload you might want to save the initial payload with the component, Payload to named property and later retrieve the original payload again with Named property to payload.
The workflow need to have "hello" setup as the path
package com.interform400.testserver;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Simple example of calling the web-service in NG2
* @author urj
*/
public class ExampleWebservice {
public static void main(String args[]) throws Exception {
callWebserice();
}
private static void callWebserice() throws Exception {
String token = login();
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost("http://localhost:8086/webservice/hello");
post.addHeader("Authorization", "Bearer " + token);
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
String xml = "<xml>Hello world</xml>";
urlParameters.add(new BasicNameValuePair("xml", xml));
post.setEntity(new UrlEncodedFormEntity(urlParameters));
CloseableHttpResponse response = httpClient.execute(post);
System.out.println(response.getStatusLine().getStatusCode());
}
private static String login() throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost("http://localhost:8086/oauth/token");
String clientId = "interform";
String clientSecret = "Io14oarPPnv3Bso10bagGA9Ovns2lvxt";
String basicAuthString = clientId + ":" + clientSecret;
String encodedCredentials = Base64.getEncoder().encodeToString(basicAuthString.getBytes());
post.addHeader("authorization", "Basic " + encodedCredentials);
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("username", "default/home"));
urlParameters.add(new BasicNameValuePair("password", "password"));
urlParameters.add(new BasicNameValuePair("grant_type", "password"));
post.setEntity(new UrlEncodedFormEntity(urlParameters));
CloseableHttpResponse response = httpClient.execute(post);
InputStream is = response.getEntity().getContent();
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(is);
String accessToken = jsonNode.get("access_token").asText();
return accessToken;
}
}
The workflow to be called need to have setup "cstest" as the Path for this demo source
using System;
using System.Text;
using System.Text.Json;
using System.Net.Http;
using System.Collections.Generic;
// InterFormNG2 web service client example
// Requires .NET v4.5+
namespace DotNetWs
{
class InterFormNGWs
{
private static readonly string serverURL = "http://localhost:8086";
private static readonly HttpClient client = new HttpClient();
public static WebToken DoLogin(string username, string password)
{
Dictionary<string, string> values = new Dictionary<string, string>
{
{ "grant_type", "password" },
{ "username", username },
{ "password", password }
};
FormUrlEncodedContent content = new FormUrlEncodedContent(values);
client.DefaultRequestHeaders.Add("Authorization",
"Basic " + Base64Encode("interform:Io14oarPPnv3Bso10bagGA9Ovns2lvxt"));
HttpResponseMessage response = client.PostAsync(serverURL + "/oauth/token", content).Result;
string responseString = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(responseString);
WebToken token = JsonSerializer.Deserialize<WebToken>(responseString);
return token;
}
public static void CallWsMultipart(string endpoint, string xmlInput, WebToken webToken)
{
MultipartFormDataContent content = new MultipartFormDataContent();
content.Add(new ByteArrayContent(Encoding.UTF8.GetBytes(xmlInput)), "xml", "testxml.xml");
content.Add(new StringContent("true"), "report");
client.DefaultRequestHeaders.Remove("Authorization");
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + webToken.access_token);
HttpResponseMessage response = client.PostAsync(serverURL + "/webservice/" + endpoint, content).Result;
string responseString = response.Content.ReadAsStringAsync().Result;
Console.WriteLine();
Console.WriteLine("Report:");
Console.WriteLine(responseString);
}
public static void CallWsUrlEncoded(string endpoint, string xmlInput, WebToken webToken)
{
Dictionary<string, string> values = new Dictionary<string, string>
{
{ "xml", xmlInput },
{ "report", "true" }
};
FormUrlEncodedContent content = new FormUrlEncodedContent(values);
client.DefaultRequestHeaders.Remove("Authorization");
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + webToken.access_token);
HttpResponseMessage response = client.PostAsync(serverURL + "/webservice/" + endpoint, content).Result;
string responseString = response.Content.ReadAsStringAsync().Result;
Console.WriteLine();
Console.WriteLine("Report:");
Console.WriteLine(responseString);
}
private static string Base64Encode(string plainText)
{
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
return Convert.ToBase64String(plainTextBytes);
}
static void Main(string[] args)
{
WebToken webToken = DoLogin("default", "password");
string xmlDoc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<root>Hello World</root>";
CallWsMultipart("cstest", xmlDoc, webToken); // Recommended multipart post
CallWsUrlEncoded("cstest", xmlDoc, webToken); // Alternative URL-encoded post
}
}
public class WebToken
{
public string access_token { get; set; }
public string refresh_token { get; set; }
public int expires_in { get; set; }
}
}