Send Embedded Object as File attachment to 3rd party Web Service

	public String sendFileAttachment(EmbeddedObject eo) {
		// General Variables
		String result = "";
		String fileName = "";

		// Web Service Variables
		URL url = null;
		HttpURLConnection httpConn = null;
		InputStream inputStream = null;
		OutputStream outputStream = null;
		BufferedReader br = null;
		byte[] buffer = null;
		int bytesRead = 0;

		// TODO: Modify the Endpoint URL and Request Type as needed
		String endpointURL = "http://localhost/uploadFile";
		String requestType = "POST";

		try {
			fileName = eo.getName();

			// Setup Web Service
			url = new URL(endpointURL);
			httpConn = (HttpURLConnection) url.openConnection();

			// This example is specific to This is an example of a 3rd party service you would connect to
			// TODO: Set the Header Properties based on the 3rd Party Web Service requirements
			httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			httpConn.setRequestProperty("Authorization", "agilite_token_key");
			httpConn.setRequestProperty("fileName", fileName);

			// Prep File for Sending
			outputStream = httpConn.getOutputStream();
			inputStream = eo.getInputStream();
			buffer = new byte[4096];
			bytesRead = -1;
			while ((bytesRead = != -1) {
				outputStream.write(buffer, 0, bytesRead);

			// Check response of Web Service Request
			System.out.println("Response Code = " + httpConn.getResponseCode());
			System.out.println("Response Message = " + httpConn.getResponseMessage());

			if (httpConn.getResponseCode() == 200) {
				// Success: Read Result from Web Service
				br = new BufferedReader(new InputStreamReader((httpConn.getInputStream())));
				result = br.readLine();
			} else {
				// Web Service Failed. Return Code and Message
				result = httpConn.getResponseCode() + " - " + httpConn.getResponseMessage();
				System.out.println("Request Failed");
		} catch (Exception e) {
			// Code Failed
			result = "Your Agent Failed";

		//Returns the result from the 3rd Party Web Service as a String
		return result;

This Snippet allows you to easily send an attached file in a NotesDocument to a 3rd party Web Service/Web API. Parts of this code will need to be modified based on what 3rd party service you are connecting to, but the majority of the logic remains unchanged.

In this Snippet, I'm sending a file to an Integration Service Portal I developed called Agilit-e (, which has a microservice that can accept and store any kind of file you send it.

John V Jardin
May 31, 2017 6:42 AM

All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.

No comments yetLogin first to comment...