Zig
Zig
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 Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// 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
const json_auth = try chilkat.JsonObject.init();
defer json_auth.deinit();
json_auth.loadFile("qa_data/tokens/ewayAuth.json") catch {
std.debug.print("{s}\n", .{try json_auth.getLastErrorText(alloc)});
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
const json_data = try chilkat.JsonObject.init();
defer json_data.deinit();
json_data.updateString("supplyType", "O") catch {};
json_data.updateString("subSupplyType", "1") catch {};
json_data.updateString("docType", "INV") catch {};
json_data.updateString("docNo", "AW1234-2") catch {};
json_data.updateString("docDate", "05/04/2018") catch {};
json_data.updateString("fromGstin", "09ABDC24212B1FK") catch {};
json_data.updateString("fromTrdName", "willy") catch {};
json_data.updateString("fromAddr1", "3RD CROSS NO 200 19 A") catch {};
json_data.updateString("fromAddr2", "GROUND FLOOR OZZY ROAD") catch {};
json_data.updateString("fromPlace", "BUSY TOWN") catch {};
json_data.updateNumber("fromPincode", "640033") catch {};
json_data.updateNumber("actFromStateCode", "05") catch {};
json_data.updateNumber("fromStateCode", "05") catch {};
json_data.updateString("toGstin", "01AAAASCC10BBBB") catch {};
json_data.updateString("toTrdName", "mthustra") catch {};
json_data.updateString("toAddr1", "Shrek Ogre") catch {};
json_data.updateString("toAddr2", "Basadronsil") catch {};
json_data.updateString("toPlace", "Grifl Blagar") catch {};
json_data.updateNumber("toPincode", "699988") catch {};
json_data.updateNumber("actToStateCode", "29") catch {};
json_data.updateNumber("toStateCode", "02") catch {};
json_data.updateNumber("totalValue", "5609889") catch {};
json_data.updateNumber("cgstValue", "0") catch {};
json_data.updateNumber("sgstValue", "0") catch {};
json_data.updateNumber("igstValue", "168296.67") catch {};
json_data.updateNumber("cessValue", "224395.56") catch {};
json_data.updateString("transporterId", "09ABDC24212B1FK") catch {};
json_data.updateString("transporterName", "") catch {};
json_data.updateString("transDocNo", "12332") catch {};
json_data.updateNumber("transMode", "1") catch {};
json_data.updateString("transDistance", "656") catch {};
json_data.updateString("transDocDate", "10/04/2018") catch {};
json_data.updateString("vehicleNo", "PBN4567") catch {};
json_data.updateString("vehicleType", "R") catch {};
json_data.setI(0);
json_data.updateString("itemList[i].productName", "Wheat") catch {};
json_data.updateString("itemList[i].productDesc", "Wheat") catch {};
json_data.updateNumber("itemList[i].hsnCode", "1001") catch {};
json_data.updateNumber("itemList[i].quantity", "4") catch {};
json_data.updateString("itemList[i].qtyUnit", "BOX") catch {};
json_data.updateNumber("itemList[i].cgstRate", "0") catch {};
json_data.updateNumber("itemList[i].sgstRate", "0") catch {};
json_data.updateNumber("itemList[i].igstRate", "3") catch {};
json_data.updateNumber("itemList[i].cessRate", "4") catch {};
json_data.updateNumber("itemList[i].cessAdvol", "0") catch {};
json_data.updateNumber("itemList[i].taxableAmount", "5609889") catch {};
// 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..
const json_request_body = try chilkat.JsonObject.init();
defer json_request_body.deinit();
json_request_body.updateString("action", "GENEWAYBILL") catch {};
// Setup the encryptor using the decryptedSek from the jsonAuth
const crypt = try chilkat.Crypt2.init();
defer crypt.deinit();
crypt.setCryptAlgorithm("aes");
crypt.setCipherMode("ecb");
crypt.setKeyLength(256);
crypt.setEncodedKey(try json_auth.stringOf(alloc, "decryptedSek"), "base64");
crypt.setEncodingMode("base64");
// Encrypt the jsonData and add it to our JSON request body
json_request_body.updateString("data", try crypt.encryptStringENC(alloc, try json_data.emit(alloc))) catch {};
const http = try chilkat.Http.init();
defer http.deinit();
// Add the authtoken to the request header.
// Be careful to be precise with uppercase/lowercase ("authtoken" vs "authToken")
http.setRequestHeader("authtoken", try json_auth.stringOf(alloc, "authToken"));
http.setRequestHeader("Gstin", "09ABDC24212B1FK");
http.setAccept("application/json");
// POST the request to generate an e-way bill:
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("POST", "http://ewb.wepgst.com/api/EWayBill", json_request_body, "application/json", resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
const resp_status_code = resp.getStatusCode();
std.debug.print("response status code ={d}\n", .{resp_status_code});
std.debug.print("response body:\n", .{});
std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
if (resp_status_code != 200) {
std.debug.print("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.
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.load(try resp.getBodyStr(alloc)) catch {};
const status = json.intOf("status");
std.debug.print("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"}
const sb_error = try chilkat.StringBuilder.init();
defer sb_error.deinit();
json.stringOfSb("error", sb_error) catch {};
sb_error.decode("base64", "utf-8") catch {};
std.debug.print("error: {s}\n", .{try sb_error.getAsString(alloc)});
return;
}
json.setEmitCompact(false);
std.debug.print("JSON response:\n", .{});
std.debug.print("{s}\n", .{try json.emit(alloc)});
const bd_data = try chilkat.BinData.init();
defer bd_data.deinit();
bd_data.appendEncoded(try json.stringOf(alloc, "data"), "base64") catch {};
crypt.decryptBd(bd_data) catch {};
// Decrypts to
// {"ewayBillNo":331001121234,"ewayBillDate":"24/05/2018 04:38:00 PM","validUpto":"31/05/2018 11:59:00 PM"}
const json_bill = try chilkat.JsonObject.init();
defer json_bill.deinit();
json_bill.load(try bd_data.getString(alloc, "utf-8")) catch {};
const eway_bill_no = json_bill.intOf("ewayBillNo");
std.debug.print("ewayBillNo = {d}\n", .{eway_bill_no});
const eway_bill_date = try json_bill.stringOf(alloc, "ewayBillDate");
std.debug.print("ewayBillDate = {s}\n", .{eway_bill_date});
const valid_upto = try json_bill.stringOf(alloc, "validUpto");
std.debug.print("validUpto = {s}\n", .{valid_upto});
// Sample output:
// ewayBillNo = 331001121234
// ewayBillDate = 24/05/2018 04:55:00 PM
// validUpto = 31/05/2018 11:59:00 PM
}