Rust
Rust
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 Rust Downloads
// 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
let json_auth = chilkat::JsonObject::new();
if json_auth.load_file("qa_data/tokens/ewayAuth.json").is_err() {
println!("{}", json_auth.last_error_text());
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
let json_data = chilkat::JsonObject::new();
let _ = json_data.update_string("supplyType", "O");
let _ = json_data.update_string("subSupplyType", "1");
let _ = json_data.update_string("docType", "INV");
let _ = json_data.update_string("docNo", "AW1234-2");
let _ = json_data.update_string("docDate", "05/04/2018");
let _ = json_data.update_string("fromGstin", "09ABDC24212B1FK");
let _ = json_data.update_string("fromTrdName", "willy");
let _ = json_data.update_string("fromAddr1", "3RD CROSS NO 200 19 A");
let _ = json_data.update_string("fromAddr2", "GROUND FLOOR OZZY ROAD");
let _ = json_data.update_string("fromPlace", "BUSY TOWN");
let _ = json_data.update_number("fromPincode", "640033");
let _ = json_data.update_number("actFromStateCode", "05");
let _ = json_data.update_number("fromStateCode", "05");
let _ = json_data.update_string("toGstin", "01AAAASCC10BBBB");
let _ = json_data.update_string("toTrdName", "mthustra");
let _ = json_data.update_string("toAddr1", "Shrek Ogre");
let _ = json_data.update_string("toAddr2", "Basadronsil");
let _ = json_data.update_string("toPlace", "Grifl Blagar");
let _ = json_data.update_number("toPincode", "699988");
let _ = json_data.update_number("actToStateCode", "29");
let _ = json_data.update_number("toStateCode", "02");
let _ = json_data.update_number("totalValue", "5609889");
let _ = json_data.update_number("cgstValue", "0");
let _ = json_data.update_number("sgstValue", "0");
let _ = json_data.update_number("igstValue", "168296.67");
let _ = json_data.update_number("cessValue", "224395.56");
let _ = json_data.update_string("transporterId", "09ABDC24212B1FK");
let _ = json_data.update_string("transporterName", "");
let _ = json_data.update_string("transDocNo", "12332");
let _ = json_data.update_number("transMode", "1");
let _ = json_data.update_string("transDistance", "656");
let _ = json_data.update_string("transDocDate", "10/04/2018");
let _ = json_data.update_string("vehicleNo", "PBN4567");
let _ = json_data.update_string("vehicleType", "R");
json_data.set_i(0);
let _ = json_data.update_string("itemList[i].productName", "Wheat");
let _ = json_data.update_string("itemList[i].productDesc", "Wheat");
let _ = json_data.update_number("itemList[i].hsnCode", "1001");
let _ = json_data.update_number("itemList[i].quantity", "4");
let _ = json_data.update_string("itemList[i].qtyUnit", "BOX");
let _ = json_data.update_number("itemList[i].cgstRate", "0");
let _ = json_data.update_number("itemList[i].sgstRate", "0");
let _ = json_data.update_number("itemList[i].igstRate", "3");
let _ = json_data.update_number("itemList[i].cessRate", "4");
let _ = json_data.update_number("itemList[i].cessAdvol", "0");
let _ = json_data.update_number("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..
let json_request_body = chilkat::JsonObject::new();
let _ = json_request_body.update_string("action", "GENEWAYBILL");
// Setup the encryptor using the decryptedSek from the jsonAuth
let crypt = chilkat::Crypt2::new();
crypt.set_crypt_algorithm("aes");
crypt.set_cipher_mode("ecb");
crypt.set_key_length(256);
crypt.set_encoded_key(&json_auth.string_of("decryptedSek").unwrap_or_default(), "base64");
crypt.set_encoding_mode("base64");
// Encrypt the jsonData and add it to our JSON request body
let _ = json_request_body.update_string("data", &crypt.encrypt_string_enc(&json_data.emit().unwrap_or_default()).unwrap_or_default());
let http = chilkat::Http::new();
// Add the authtoken to the request header.
// Be careful to be precise with uppercase/lowercase ("authtoken" vs "authToken")
http.set_request_header("authtoken", &json_auth.string_of("authToken").unwrap_or_default());
http.set_request_header("Gstin", "09ABDC24212B1FK");
http.set_accept("application/json");
// POST the request to generate an e-way bill:
let resp = chilkat::HttpResponse::new();
if http.http_json("POST", "http://ewb.wepgst.com/api/EWayBill", &json_request_body, "application/json", &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
let resp_status_code = resp.status_code();
println!("response status code ={}", resp_status_code);
println!("response body:");
println!("{}", resp.body_str());
if resp_status_code != 200 {
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.
let json = chilkat::JsonObject::new();
let _ = json.load(&resp.body_str());
let status = json.int_of("status");
println!("status = {}", status);
if status != 1 {
// Failed. Base64 decode the error
// {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
// For an invalid password, the error is: {"errorCodes":"108"}
let sb_error = chilkat::StringBuilder::new();
let _ = json.string_of_sb("error", &sb_error);
let _ = sb_error.decode("base64", "utf-8");
println!("error: {}", sb_error.get_as_string().unwrap_or_default());
return;
}
json.set_emit_compact(false);
println!("JSON response:");
println!("{}", json.emit().unwrap_or_default());
let bd_data = chilkat::BinData::new();
let _ = bd_data.append_encoded(&json.string_of("data").unwrap_or_default(), "base64");
let _ = crypt.decrypt_bd(&bd_data);
// Decrypts to
// {"ewayBillNo":331001121234,"ewayBillDate":"24/05/2018 04:38:00 PM","validUpto":"31/05/2018 11:59:00 PM"}
let json_bill = chilkat::JsonObject::new();
let _ = json_bill.load(&bd_data.get_string("utf-8").unwrap_or_default());
let eway_bill_no = json_bill.int_of("ewayBillNo");
println!("ewayBillNo = {}", eway_bill_no);
let eway_bill_date = json_bill.string_of("ewayBillDate").unwrap_or_default();
println!("ewayBillDate = {}", eway_bill_date);
let valid_upto = json_bill.string_of("validUpto").unwrap_or_default();
println!("validUpto = {}", valid_upto);
// Sample output:
// ewayBillNo = 331001121234
// ewayBillDate = 24/05/2018 04:55:00 PM
// validUpto = 31/05/2018 11:59:00 PM