- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 09-05-2017 08:59 AM
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<Header> headers = new ArrayList<Header>();
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<String> 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<String> responseHandler=new BasicResponseHandler();
String responseBody = client.execute(post, responseHandler);
System.out.println(responseBody);
} catch (Exception e) {
e.printStackTrace();
}
}
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thank you very much Nithu!
I was trying same on my own and was stuck. Looked at your code found error in mine
I was using builder.addTextBody instead of addPart.
Thanks Again!
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Wonderful!! I am glad it was helpful to someone.
Thanks!
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thank you Nithu!
Useful and informative!
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I am trying to upload the binary file below is the code, it keep failing with Bad request error, please suggest what could be the issue.
HttpPost httpPost = new HttpPost(attachmenturi);
if (!byPassProxy) httpPost.setConfig(config);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody bin = new FileBody(attachfile,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", bin);
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
{
"assignment_group": "378e2fa9374c6200772863d2b3990e0d",
"attachment_flg": true,
"attachment_name": "Lottery.jpg",
"attachment_path": "e://temp//Lottery.jpg",
"batch": true,
"description": "PPlease find attached, the Q05 GWP Client Site Monthly Response Time Report",
"due_date": "20190506",
"group_list": "378e2fa9374c6200772863d2b3990e0d",
"inc_id": "string",
"inc_source": "string",
"priority": "3",
"short_description": "TTesting",
"user": "ssaif",
"watch_list": "378e2fa9374c6200772863d2b3990e0d"
}
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Does anyone have sample using HTTPUrlConnection
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
this i tried sending my file text file or image file. it does not go in proper format, file get upload to service-now but when we download that file it says format does not support or some content-composition values gets added to the text file(when we upload the text file)
Please suggest is it only issue with Tokyo version or something else