Cloud - Get Template Sample Data using Java via API

This example shows how to get sample data for a Template in JSON format using Java.  It calls the Docmosis REST API to return sample data suitable to populate the "WelcomeTemplate.docx" template.

The sample code includes the instructions to get started. You will need a Free Trial and then plug your Docmosis Cloud access key into the code below, then run.

Note: This code sample is written to specifically work with DWS4.

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;

/**
 * This sample code shows a call to Docmosis to get sample data suitable to populate
 * the  "WelcomeTemplate.docx" template.  
 *  
 * The response is a JSON structure and it contains a sub-structure of data matching
 * the template.
 * 
 * Copyright Docmosis 2019
 */
public class GetTemplateSampleDataExample 
{

	// Set your region here.
	// End-point in the USA
	private static final String URL = "https://us1.dws4.docmosis.com/api/getSampleData";
	// End-point in the EU
	//private static final String URL = "https://eu1.dws4.docmosis.com/api/getSampleData";
	// End-point in AUS
	//private static final String URL = "https://au1.dws4.docmosis.com/api/getSampleData";
	
	// Set your access key here. This is visible in your cloud account in the Docmosis Web Site.
	// It is your key to accessing your service - keep it private and safe.
	private static final String ACCESS_KEY = "XXX";

	/*
	 * Run this example
	 */
	public static void main(String[] args) throws MalformedURLException,
			IOException 
	{

		if ("XXX".equals(ACCESS_KEY)) {
			System.err.println("You must set the access key");
			System.exit(1);
		}

		HttpURLConnection conn = null;
		DataOutputStream os = null;
		try {
			conn = (HttpURLConnection) new URL(URL).openConnection();
			System.out.println("Connecting [directly] to " + URL);

			// set connection parameters
			conn.setRequestMethod("POST");
			conn.setUseCaches(false);
			conn.setDoOutput(true);
			conn.setDoInput(true);

			// this example uses JSON format
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

			conn.connect();
			System.out.println("Connected");

			// build request
			final String templateName = "samples/WelcomeTemplate.docx";
			
			final String params = "accessKey=" + ACCESS_KEY + "&templateName=" + templateName;
			System.out.println("Sending request:" + params);
			final byte[] postData = params.getBytes( Charset.forName("UTF-8") );
			
			// send the parameters
			os = new DataOutputStream(conn.getOutputStream());
			os.write(postData);
			os.flush();
			
			// get the response
			int status = conn.getResponseCode();
			if (status == 200) {
				// successful call, extract the response
				
				BufferedReader br = new BufferedReader(
						new InputStreamReader(conn.getInputStream()));
				StringBuffer sb = new StringBuffer();
				String msg;
				while ((msg = br.readLine()) != null) {
					sb.append(msg);
				}
				System.out.println("response:" + sb.toString());
				
				// add code here to process the response as JSON and 
				// in particular the "templateSampleData" field.
				
				br = null;
			} else {
				// something went wrong - tell the user
				System.err.println("Our call failed: status = " + status);
				System.err.println("message:" + conn.getResponseMessage());
				BufferedReader errorReader = new BufferedReader(
						new InputStreamReader(conn.getErrorStream()));
				String msg;
				while ((msg = errorReader.readLine()) != null) {
					System.err.println(msg);
				}
				errorReader = null;
			}
			
		} catch (ConnectException e) {
			// can't make the connection
			System.err.println("Unable to connect to Docmosis:" + e.getMessage());
			System.exit(2);
		} finally {
			if (os != null) {
				os.close();
			}
			if (conn != null) {
				conn.disconnect();
			}
		}

	}
}

Feedback

Invalid Input

Sorry, this field will only accept letters and numbers, and not special characters, to limit spam. Please also consider contacting support@docmosis.com if you need help with this article.

Sorry, this field will only accept letters and numbers, and not special characters, to limit spam.

Invalid Input

API Overview

Docmosis offers a high-performance, template-based PDF generation API. Suitable for use with custom software applications built using Java, C#/.Net, Python, Ruby and more. See a side-by-side comparison of the API for our SaaS and self-hosted products.