Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkJsonObject.h>
#include <C_CkCrypt2.h>
#include <C_CkHttp.h>
#include <C_CkHttpResponse.h>
#include <C_CkStringBuilder.h>
#include <C_CkBinData.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObject jsonAuth;
    HCkJsonObject jsonData;
    HCkJsonObject jsonRequestBody;
    HCkCrypt2 crypt;
    HCkHttp http;
    HCkHttpResponse resp;
    int respStatusCode;
    HCkJsonObject json;
    int status;
    HCkStringBuilder sbError;
    HCkBinData bdData;
    HCkJsonObject jsonBill;
    int ewayBillNo;
    const char *ewayBillDate;
    const char *validUpto;

    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

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

    // Setup the encryptor using the decryptedSek from the jsonAuth
    crypt = CkCrypt2_Create();
    CkCrypt2_putCryptAlgorithm(crypt,"aes");
    CkCrypt2_putCipherMode(crypt,"ecb");
    CkCrypt2_putKeyLength(crypt,256);
    CkCrypt2_SetEncodedKey(crypt,CkJsonObject_stringOf(jsonAuth,"decryptedSek"),"base64");
    CkCrypt2_putEncodingMode(crypt,"base64");

    // Encrypt the jsonData and add it to our JSON request body
    CkJsonObject_UpdateString(jsonRequestBody,"data",CkCrypt2_encryptStringENC(crypt,CkJsonObject_emit(jsonData)));

    http = CkHttp_Create();

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

    // POST the request to generate an e-way bill:
    resp = CkHttpResponse_Create();
    success = CkHttp_HttpJson(http,"POST","http://ewb.wepgst.com/api/EWayBill",jsonRequestBody,"application/json",resp);
    if (success == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkJsonObject_Dispose(jsonAuth);
        CkJsonObject_Dispose(jsonData);
        CkJsonObject_Dispose(jsonRequestBody);
        CkCrypt2_Dispose(crypt);
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        return;
    }

    respStatusCode = CkHttpResponse_getStatusCode(resp);
    printf("response status code =%d\n",respStatusCode);
    printf("response body:\n");
    printf("%s\n",CkHttpResponse_bodyStr(resp));

    if (respStatusCode != 200) {
        printf("Failed in some unknown way.\n");
        CkJsonObject_Dispose(jsonAuth);
        CkJsonObject_Dispose(jsonData);
        CkJsonObject_Dispose(jsonRequestBody);
        CkCrypt2_Dispose(crypt);
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        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.
    json = CkJsonObject_Create();
    CkJsonObject_Load(json,CkHttpResponse_bodyStr(resp));

    status = CkJsonObject_IntOf(json,"status");
    printf("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"}
        sbError = CkStringBuilder_Create();
        CkJsonObject_StringOfSb(json,"error",sbError);
        CkStringBuilder_Decode(sbError,"base64","utf-8");
        printf("error: %s\n",CkStringBuilder_getAsString(sbError));
        CkJsonObject_Dispose(jsonAuth);
        CkJsonObject_Dispose(jsonData);
        CkJsonObject_Dispose(jsonRequestBody);
        CkCrypt2_Dispose(crypt);
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(json);
        CkStringBuilder_Dispose(sbError);
        return;
    }

    CkJsonObject_putEmitCompact(json,FALSE);
    printf("JSON response:\n");
    printf("%s\n",CkJsonObject_emit(json));

    bdData = CkBinData_Create();
    CkBinData_AppendEncoded(bdData,CkJsonObject_stringOf(json,"data"),"base64");
    CkCrypt2_DecryptBd(crypt,bdData);

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

    jsonBill = CkJsonObject_Create();
    CkJsonObject_Load(jsonBill,CkBinData_getString(bdData,"utf-8"));

    ewayBillNo = CkJsonObject_IntOf(jsonBill,"ewayBillNo");
    printf("ewayBillNo = %d\n",ewayBillNo);

    ewayBillDate = CkJsonObject_stringOf(jsonBill,"ewayBillDate");
    printf("ewayBillDate = %s\n",ewayBillDate);

    validUpto = CkJsonObject_stringOf(jsonBill,"validUpto");
    printf("validUpto = %s\n",validUpto);

    // Sample output:

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


    CkJsonObject_Dispose(jsonAuth);
    CkJsonObject_Dispose(jsonData);
    CkJsonObject_Dispose(jsonRequestBody);
    CkCrypt2_Dispose(crypt);
    CkHttp_Dispose(http);
    CkHttpResponse_Dispose(resp);
    CkJsonObject_Dispose(json);
    CkStringBuilder_Dispose(sbError);
    CkBinData_Dispose(bdData);
    CkJsonObject_Dispose(jsonBill);

    }