Sample code for 30+ languages & platforms
Unicode C++

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 Unicode C++ Downloads

Unicode C++
#include <CkJsonObjectW.h>
#include <CkCrypt2W.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkStringBuilderW.h>
#include <CkBinDataW.h>

void ChilkatSample(void)
    {
    bool success = false;

    // 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

    CkJsonObjectW jsonAuth;
    success = jsonAuth.LoadFile(L"qa_data/tokens/ewayAuth.json");
    if (success == false) {
        wprintf(L"%s\n",jsonAuth.lastErrorText());
        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
    CkJsonObjectW jsonData;
    jsonData.UpdateString(L"supplyType",L"O");
    jsonData.UpdateString(L"subSupplyType",L"1");
    jsonData.UpdateString(L"docType",L"INV");
    jsonData.UpdateString(L"docNo",L"AW1234-2");
    jsonData.UpdateString(L"docDate",L"05/04/2018");
    jsonData.UpdateString(L"fromGstin",L"09ABDC24212B1FK");
    jsonData.UpdateString(L"fromTrdName",L"willy");
    jsonData.UpdateString(L"fromAddr1",L"3RD CROSS NO 200  19  A");
    jsonData.UpdateString(L"fromAddr2",L"GROUND FLOOR OZZY ROAD");
    jsonData.UpdateString(L"fromPlace",L"BUSY TOWN");
    jsonData.UpdateNumber(L"fromPincode",L"640033");
    jsonData.UpdateNumber(L"actFromStateCode",L"05");
    jsonData.UpdateNumber(L"fromStateCode",L"05");
    jsonData.UpdateString(L"toGstin",L"01AAAASCC10BBBB");
    jsonData.UpdateString(L"toTrdName",L"mthustra");
    jsonData.UpdateString(L"toAddr1",L"Shrek Ogre");
    jsonData.UpdateString(L"toAddr2",L"Basadronsil");
    jsonData.UpdateString(L"toPlace",L"Grifl Blagar");
    jsonData.UpdateNumber(L"toPincode",L"699988");
    jsonData.UpdateNumber(L"actToStateCode",L"29");
    jsonData.UpdateNumber(L"toStateCode",L"02");
    jsonData.UpdateNumber(L"totalValue",L"5609889");
    jsonData.UpdateNumber(L"cgstValue",L"0");
    jsonData.UpdateNumber(L"sgstValue",L"0");
    jsonData.UpdateNumber(L"igstValue",L"168296.67");
    jsonData.UpdateNumber(L"cessValue",L"224395.56");
    jsonData.UpdateString(L"transporterId",L"09ABDC24212B1FK");
    jsonData.UpdateString(L"transporterName",L"");
    jsonData.UpdateString(L"transDocNo",L"12332");
    jsonData.UpdateNumber(L"transMode",L"1");
    jsonData.UpdateString(L"transDistance",L"656");
    jsonData.UpdateString(L"transDocDate",L"10/04/2018");
    jsonData.UpdateString(L"vehicleNo",L"PBN4567");
    jsonData.UpdateString(L"vehicleType",L"R");
    jsonData.put_I(0);
    jsonData.UpdateString(L"itemList[i].productName",L"Wheat");
    jsonData.UpdateString(L"itemList[i].productDesc",L"Wheat");
    jsonData.UpdateNumber(L"itemList[i].hsnCode",L"1001");
    jsonData.UpdateNumber(L"itemList[i].quantity",L"4");
    jsonData.UpdateString(L"itemList[i].qtyUnit",L"BOX");
    jsonData.UpdateNumber(L"itemList[i].cgstRate",L"0");
    jsonData.UpdateNumber(L"itemList[i].sgstRate",L"0");
    jsonData.UpdateNumber(L"itemList[i].igstRate",L"3");
    jsonData.UpdateNumber(L"itemList[i].cessRate",L"4");
    jsonData.UpdateNumber(L"itemList[i].cessAdvol",L"0");
    jsonData.UpdateNumber(L"itemList[i].taxableAmount",L"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..
    CkJsonObjectW jsonRequestBody;
    jsonRequestBody.UpdateString(L"action",L"GENEWAYBILL");

    // Setup the encryptor using the decryptedSek from the jsonAuth
    CkCrypt2W crypt;
    crypt.put_CryptAlgorithm(L"aes");
    crypt.put_CipherMode(L"ecb");
    crypt.put_KeyLength(256);
    crypt.SetEncodedKey(jsonAuth.stringOf(L"decryptedSek"),L"base64");
    crypt.put_EncodingMode(L"base64");

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

    CkHttpW http;

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

    // POST the request to generate an e-way bill:
    CkHttpResponseW resp;
    success = http.HttpJson(L"POST",L"http://ewb.wepgst.com/api/EWayBill",jsonRequestBody,L"application/json",resp);
    if (success == false) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    int respStatusCode = resp.get_StatusCode();
    wprintf(L"response status code =%d\n",respStatusCode);
    wprintf(L"response body:\n");
    wprintf(L"%s\n",resp.bodyStr());

    if (respStatusCode != 200) {
        wprintf(L"Failed in some unknown way.\n");
        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.
    CkJsonObjectW json;
    json.Load(resp.bodyStr());

    int status = json.IntOf(L"status");
    wprintf(L"status = %d\n",status);

    if (status != 1) {
        // Failed.  Base64 decode the error
        // {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
        // For an invalid password, the error is: {"errorCodes":"108"}
        CkStringBuilderW sbError;
        json.StringOfSb(L"error",sbError);
        sbError.Decode(L"base64",L"utf-8");
        wprintf(L"error: %s\n",sbError.getAsString());
        return;
    }

    json.put_EmitCompact(false);
    wprintf(L"JSON response:\n");
    wprintf(L"%s\n",json.emit());

    CkBinDataW bdData;
    bdData.AppendEncoded(json.stringOf(L"data"),L"base64");
    crypt.DecryptBd(bdData);

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

    CkJsonObjectW jsonBill;
    jsonBill.Load(bdData.getString(L"utf-8"));

    int ewayBillNo = jsonBill.IntOf(L"ewayBillNo");
    wprintf(L"ewayBillNo = %d\n",ewayBillNo);

    const wchar_t *ewayBillDate = jsonBill.stringOf(L"ewayBillDate");
    wprintf(L"ewayBillDate = %s\n",ewayBillDate);

    const wchar_t *validUpto = jsonBill.stringOf(L"validUpto");
    wprintf(L"validUpto = %s\n",validUpto);

    // Sample output:

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