logo

NJP

Attachment API REST call using Java

Import · Sep 05, 2017 · article

There are some examples available to call the attachment API via "Python" or "Jersey" scripts. However I was not able to find a good reference on how to call the API using Java. So posting an example here for reference, using "Java" and "Apache Http client".

Send a binary attachment using file upload

public static void main(String[] args) {

try {

//Add needed headers

Header header = new BasicHeader(HttpHeaders.CONTENT_TYPE, "image/jpeg");

Header header2 = new BasicHeader(HttpHeaders.ACCEPT, "application/json");

List

headers = new ArrayList();

headers.add(header);

headers.add(header2);

//Add credential

CredentialsProvider provider = new BasicCredentialsProvider();

UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("yourUserName", "yourPassword");

provider.setCredentials(AuthScope.ANY, credentials);

CloseableHttpClient client = HttpClientBuilder.create()

.setDefaultCredentialsProvider(provider)

.setDefaultHeaders(headers)

.build();

HttpPost post = new HttpPost("https://yourInstanceName/api/now/attachment/file?table_name=incident&table_sys_id=c8c3ba6643333a4073...");

String textFileName = "C:/Users/Public/Pictures/Sample Pictures/Tulips.jpg";

byte[] fileBytes = getBytes(textFileName);

post.setEntity(new ByteArrayEntity(fileBytes));

ResponseHandler responseHandler=new BasicResponseHandler();

String responseBody = client.execute(post, responseHandler);

System.out.println(responseBody);

} catch (Exception e) {

e.printStackTrace();

}

}

private static byte[] getBytes(String filepath) throws Exception{

File file = new File(filepath);

//init array with file length

byte[] bytesArray = new byte[(int) file.length()];

FileInputStream fis = new FileInputStream(file);

fis.read(bytesArray); //read file into bytes[]

fis.close();

return bytesArray;

}

Send a binary file using Multipart File Upload

public static void main(String[] args) {

try {

CredentialsProvider provider = new BasicCredentialsProvider();

UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("yourUserName", "yourPassword");

provider.setCredentials(AuthScope.ANY, credentials);

CloseableHttpClient client = HttpClientBuilder.create()

.setDefaultCredentialsProvider(provider)

.build();

HttpPost post = new HttpPost("https://yourInstanceName/api/now/attachment/upload");

String textFileName = "C:/Users/Public/Pictures/Sample Pictures/Tulips.jpg";

File file = new File(textFileName);

MultipartEntityBuilder builder = MultipartEntityBuilder.create();

builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);

StringBody stringBody1 = new StringBody("incident", ContentType.MULTIPART_FORM_DATA);

StringBody stringBody2 = new StringBody("c8c3ba6643333a4073a1433e0210c706", ContentType.MULTIPART_FORM_DATA);

builder.addPart("table_name", stringBody1);

builder.addPart("table_sys_id", stringBody2);

builder.addPart("f", fileBody);

HttpEntity entity = builder.build();

post.setEntity(entity);

// Execute HTTP Post Request

ResponseHandler responseHandler=new BasicResponseHandler();

String responseBody = client.execute(post, responseHandler);

System.out.println(responseBody);

} catch (Exception e) {

e.printStackTrace();

}

}

View original source

https://www.servicenow.com/community/developer-articles/attachment-api-rest-call-using-java/ta-p/2329743