Sample code for 30+ languages & platforms
Java

Generate an E-way Bill

See more HTTP Misc Examples

Demonstrates how to send an HTTP POST request to generate an e-way bill.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Crypt2;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // This example uses the previously obtained access token that was retrieved
        // in this example:  Get EWAY Auth Token using Gstin, username, password, and app_key

        JsonObject jsonAuth = new JsonObject();
        try {
            jsonAuth.loadFile("qa_data/tokens/ewayAuth.json");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // The jsonAuth contains something like this:
        // {
        //   "authToken": "IBTeFtxNfVurg71LTzZ2r0xK7",
        //   "decryptedSek": "5g1TyTie7yoslU3DrbYATa7mWyPazlODE7cEh5Vy4Ho="
        // }

        // Generate the JSON data for the e-way bill.
        // The following code can be generated by pasting representative JSON into this online tool:
        // Generate JSON Code
        JsonObject jsonData = new JsonObject();
        jsonData.updateString("supplyType", "O");
        jsonData.updateString("subSupplyType", "1");
        jsonData.updateString("docType", "INV");
        jsonData.updateString("docNo", "AW1234-2");
        jsonData.updateString("docDate", "05/04/2018");
        jsonData.updateString("fromGstin", "09ABDC24212B1FK");
        jsonData.updateString("fromTrdName", "willy");
        jsonData.updateString("fromAddr1", "3RD CROSS NO 200  19  A");
        jsonData.updateString("fromAddr2", "GROUND FLOOR OZZY ROAD");
        jsonData.updateString("fromPlace", "BUSY TOWN");
        jsonData.updateNumber("fromPincode", "640033");
        jsonData.updateNumber("actFromStateCode", "05");
        jsonData.updateNumber("fromStateCode", "05");
        jsonData.updateString("toGstin", "01AAAASCC10BBBB");
        jsonData.updateString("toTrdName", "mthustra");
        jsonData.updateString("toAddr1", "Shrek Ogre");
        jsonData.updateString("toAddr2", "Basadronsil");
        jsonData.updateString("toPlace", "Grifl Blagar");
        jsonData.updateNumber("toPincode", "699988");
        jsonData.updateNumber("actToStateCode", "29");
        jsonData.updateNumber("toStateCode", "02");
        jsonData.updateNumber("totalValue", "5609889");
        jsonData.updateNumber("cgstValue", "0");
        jsonData.updateNumber("sgstValue", "0");
        jsonData.updateNumber("igstValue", "168296.67");
        jsonData.updateNumber("cessValue", "224395.56");
        jsonData.updateString("transporterId", "09ABDC24212B1FK");
        jsonData.updateString("transporterName", "");
        jsonData.updateString("transDocNo", "12332");
        jsonData.updateNumber("transMode", "1");
        jsonData.updateString("transDistance", "656");
        jsonData.updateString("transDocDate", "10/04/2018");
        jsonData.updateString("vehicleNo", "PBN4567");
        jsonData.updateString("vehicleType", "R");
        jsonData.setI(0);
        jsonData.updateString("itemList[i].productName", "Wheat");
        jsonData.updateString("itemList[i].productDesc", "Wheat");
        jsonData.updateNumber("itemList[i].hsnCode", "1001");
        jsonData.updateNumber("itemList[i].quantity", "4");
        jsonData.updateString("itemList[i].qtyUnit", "BOX");
        jsonData.updateNumber("itemList[i].cgstRate", "0");
        jsonData.updateNumber("itemList[i].sgstRate", "0");
        jsonData.updateNumber("itemList[i].igstRate", "3");
        jsonData.updateNumber("itemList[i].cessRate", "4");
        jsonData.updateNumber("itemList[i].cessAdvol", "0");
        jsonData.updateNumber("itemList[i].taxableAmount", "5609889");

        // The body of the HTTP POST will contain JSON that looks like this:
        //     {
        //     "action":"GENEWAYBILL",
        //     "data": " iJiJGXq ... oUZp/25Y "
        //     }

        // The "data" is the encrypted jsonData using our previously agreed-upon symmetric encryption key.
        // Let's begin build the JSON request body..
        JsonObject jsonRequestBody = new JsonObject();
        jsonRequestBody.updateString("action", "GENEWAYBILL");

        // Setup the encryptor using the decryptedSek from the jsonAuth
        Crypt2 crypt = new Crypt2();
        crypt.setCryptAlgorithm("aes");
        crypt.setCipherMode("ecb");
        crypt.setKeyLength(256);
        crypt.setEncodedKey(jsonAuth.stringOf("decryptedSek"), "base64");
        crypt.setEncodingMode("base64");

        // Encrypt the jsonData and add it to our JSON request body
        jsonRequestBody.updateString("data", crypt.encryptStringENC(jsonData.emit()));

        Http http = new Http();

        // Add the authtoken to the request header.
        // Be careful to be precise with uppercase/lowercase ("authtoken" vs "authToken")
        http.setRequestHeader("authtoken", jsonAuth.stringOf("authToken"));
        http.setRequestHeader("Gstin", "09ABDC24212B1FK");
        http.setAccept("application/json");

        // POST the request to generate an e-way bill:
        HttpResponse resp = new HttpResponse();
        try {
            http.httpJson("POST", "http://ewb.wepgst.com/api/EWayBill", jsonRequestBody, "application/json", resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        int respStatusCode = resp.getStatusCode();
        System.out.println("response status code =" + respStatusCode);
        System.out.println("response body:");
        System.out.println(resp.getBodyStr());

        if (respStatusCode != 200) {
            System.out.println("Failed in some unknown way.");
            return;
        }

        // When the response status code = 200, we'll have either
        // success response like this:
        //  {"status":"1","data":"..."}
        //
        // or a failed response like this:
        //
        // {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}

        // Load the response body into a JSON object.
        JsonObject json = new JsonObject();
        json.load(resp.getBodyStr());

        int status = json.intOf("status");
        System.out.println("status = " + status);

        if (status != 1) {
            // Failed.  Base64 decode the error
            // {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
            // For an invalid password, the error is: {"errorCodes":"108"}
            StringBuilder sbError = new StringBuilder();
            json.stringOfSb("error", sbError);
            sbError.decode("base64", "utf-8");
            System.out.println("error: " + sbError.getAsString());
            return;
        }

        json.setEmitCompact(false);
        System.out.println("JSON response:");
        System.out.println(json.emit());

        BinData bdData = new BinData();
        bdData.appendEncoded(json.stringOf("data"), "base64");
        crypt.decryptBd(bdData);

        // Decrypts to
        // {"ewayBillNo":331001121234,"ewayBillDate":"24/05/2018 04:38:00 PM","validUpto":"31/05/2018 11:59:00 PM"}

        JsonObject jsonBill = new JsonObject();
        jsonBill.load(bdData.getString("utf-8"));

        int ewayBillNo = jsonBill.intOf("ewayBillNo");
        System.out.println("ewayBillNo = " + ewayBillNo);

        String ewayBillDate = jsonBill.stringOf("ewayBillDate");
        System.out.println("ewayBillDate = " + ewayBillDate);

        String validUpto = jsonBill.stringOf("validUpto");
        System.out.println("validUpto = " + validUpto);

        // Sample output:

        // ewayBillNo = 331001121234
        // ewayBillDate = 24/05/2018 04:55:00 PM
        // validUpto = 31/05/2018 11:59:00 PM
    }
}