Attach Document To Waybill

Overview

Service Unique Name AttachDocumentToWaybill
Request Method POST
Content Type application/x-www-form-urlencoded; charset=utf-8

Description

Attach document to waybill API is used to attach a document like invoice or any additional information related to shipment after the creation of waybills.

Request Header

AccessKey : logixerp

Request Body

These are form parameters:

  • file : file byte stream
  • waybillNumber : waybill number against which document is to be attached
  • fileType : "jpeg" or "jpg" or "pdf"
  • fileCategory : File category as available in the logixerp.

Request Details

Parameter Data Type Required/Optional Description
SecureKey int Required It is a Unique key for accessing the API. For secure key email to support team.
File String Required File in which you have enter details of document which you want to add.
WaybillNumber String Required Waybill Number is against which document is to be attached.
FileType String Required File can be in jpeg or jpg or pdf format only.
FileCategory String Optional File Category is same as available in the logixerp.

Response Format

ON SUCCESS
{
"message" : "File upload successfully",
"messageType" : "Success"
}

ON ERROR
{
"message" : "Incorrect file type",
"messageType" : "Error"
}
{
"message" : "Missing file",
"messageType" : "Error"
}

Error Message String

Error Messages Description
Incorrect File Type When select file format is incorrect.
Missing File When file is not selected.

Sample Code

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import java.util.Set;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;

import javax.imageio.stream.ImageInputStream;

public class AttachDocumentToWaybill {

private static final String API_URL = "https://api.logixplatform.com/Rachna/webservice/v2/AttachDocumentToWaybill?secureKey=";
private static final int SECURE_KEY = -1280192700;

private static final String ACCESS_URL = API_URL+SECURE_KEY;

URL url;
HttpURLConnection conn;
String boundary = "--------httppost123";
Map textParams = new HashMap();
Map fileparams = new HashMap();

DataOutputStream ds;

public AttachDocumentToWaybill(String url) throws Exception {
this.url = new URL(url);
}
// Reset the server address to be requested, that is, the address of the uploaded file.
public void setUrl(String url) throws Exception {
this.url = new URL(url);
}
// Add a normal string data to the form form data
public void addTextParameter(String name, String value) {
textParams.put(name, value);
}
// Add a file to the form form data
public void addFileParameter(String name, File value) {
fileparams.put(name, value);
}
// Clear all added form form data
public void clearAllParameters() {
textParams.clear();
fileparams.clear();
}
// Send data to the server, return a byte containing an array of the server's return results
public byte[] send() throws Exception {
initConnection();
try {
conn.connect();
} catch (SocketTimeoutException e) {
// something
throw new RuntimeException();
}
ds = new DataOutputStream(conn.getOutputStream());
writeFileParams();
writeStringParams();
paramsEnd();
InputStream in = conn.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
conn.disconnect();
return out.toByteArray();
}
// File upload connection must be set
private void initConnection() throws Exception {
conn = (HttpURLConnection) this.url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setConnectTimeout(100000000);
//Connection timeout is 10 seconds
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
conn.setRequestProperty("AccessKey", "logixerp");
}
// ordinary string data
private void writeStringParams() throws Exception {
Set keySet = textParams.keySet();
for (Iterator it = keySet.iterator(); it.hasNext();) {
String name = it.next();
String value = textParams.get(name);
ds.writeBytes("--" + boundary + "\r\n");
ds.writeBytes("Content-Disposition: form-data; name=\"" + name
+ "\"\r\n");
ds.writeBytes("\r\n");
ds.writeBytes(encode(value) + "\r\n");
}
}
// File data
private void writeFileParams() throws Exception {
Set keySet = fileparams.keySet();
for (Iterator it = keySet.iterator(); it.hasNext();) {
String name = it.next();
File value = fileparams.get(name);
ds.writeBytes("--" + boundary + "\r\n");
ds.writeBytes("Content-Disposition: form-data; name=\"" + name
+ "\"; filename=\"" + encode(value.getName()) + "\"\r\n");
ds.writeBytes("Content-Type: " + getContentType(value) + "\r\n");
ds.writeBytes("\r\n");
ds.write(getBytes(value));
ds.writeBytes("\r\n");
}
}
// Get the file upload type, the image format is image / png, image / jpg and so on. Non-image is application/octet-stream

private String getContentType(File f) throws Exception {

// return "application/octet-stream"; // This line is no longer subdivided into images, all as application/octet-stream types
ImageInputStream imagein = ImageIO.createImageInputStream(f);
if (imagein == null) {
return "application/octet-stream";
}
Iterator it = ImageIO.getImageReaders(imagein);
if (!it.hasNext()) {
imagein.close();
return "application/octet-stream";
}
imagein.close();
return "image/" + it.next().getFormatName().toLowerCase();
//Converts the value returned by FormatName to lowercase, defaults to uppercase
}
// Convert the file into a byte array
private byte[] getBytes(File f) throws Exception {
FileInputStream in = new FileInputStream(f);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = in.read(b)) != -1) {
out.write(b, 0, n);
}
in.close();
return out.toByteArray();
}
// Add end data
private void paramsEnd() throws Exception {
ds.writeBytes("--" + boundary + "--" + "\r\n");
ds.writeBytes("\r\n");
}
// Transcode a string containing Chinese, which is UTF-8. The server has to decode once
private String encode(String value) throws Exception{
return URLEncoder.encode(value, "UTF-8");
}
public static void main(String[] args) throws Exception {
AttachDocumentToWaybill2 u = new AttachDocumentToWaybill2(ACCESS_URL);
u.addFileParameter("file", new File(
"D:\\college doucument\\document\\New folder\\paper.pdf"));
u.addTextParameter("waybillNumber","DELHI13670");
u.addTextParameter("fileType","pdf");
byte[] b = u.send();
String result = new String(b);
System.out.println(result);
}
}

Just a Example

Some content.

please wait...