![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Swift) Generate an E-way BillDemonstrates how to send an HTTP POST request to generate an e-way bill.
func chilkatTest() { var success: Bool = 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 let jsonAuth = CkoJsonObject()! success = jsonAuth.loadFile(path: "qa_data/tokens/ewayAuth.json") if success == false { print("\(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 let jsonData = CkoJsonObject()! jsonData.updateString(jsonPath: "supplyType", value: "O") jsonData.updateString(jsonPath: "subSupplyType", value: "1") jsonData.updateString(jsonPath: "docType", value: "INV") jsonData.updateString(jsonPath: "docNo", value: "AW1234-2") jsonData.updateString(jsonPath: "docDate", value: "05/04/2018") jsonData.updateString(jsonPath: "fromGstin", value: "09ABDC24212B1FK") jsonData.updateString(jsonPath: "fromTrdName", value: "willy") jsonData.updateString(jsonPath: "fromAddr1", value: "3RD CROSS NO 200 19 A") jsonData.updateString(jsonPath: "fromAddr2", value: "GROUND FLOOR OZZY ROAD") jsonData.updateString(jsonPath: "fromPlace", value: "BUSY TOWN") jsonData.updateNumber(jsonPath: "fromPincode", numericStr: "640033") jsonData.updateNumber(jsonPath: "actFromStateCode", numericStr: "05") jsonData.updateNumber(jsonPath: "fromStateCode", numericStr: "05") jsonData.updateString(jsonPath: "toGstin", value: "01AAAASCC10BBBB") jsonData.updateString(jsonPath: "toTrdName", value: "mthustra") jsonData.updateString(jsonPath: "toAddr1", value: "Shrek Ogre") jsonData.updateString(jsonPath: "toAddr2", value: "Basadronsil") jsonData.updateString(jsonPath: "toPlace", value: "Grifl Blagar") jsonData.updateNumber(jsonPath: "toPincode", numericStr: "699988") jsonData.updateNumber(jsonPath: "actToStateCode", numericStr: "29") jsonData.updateNumber(jsonPath: "toStateCode", numericStr: "02") jsonData.updateNumber(jsonPath: "totalValue", numericStr: "5609889") jsonData.updateNumber(jsonPath: "cgstValue", numericStr: "0") jsonData.updateNumber(jsonPath: "sgstValue", numericStr: "0") jsonData.updateNumber(jsonPath: "igstValue", numericStr: "168296.67") jsonData.updateNumber(jsonPath: "cessValue", numericStr: "224395.56") jsonData.updateString(jsonPath: "transporterId", value: "09ABDC24212B1FK") jsonData.updateString(jsonPath: "transporterName", value: "") jsonData.updateString(jsonPath: "transDocNo", value: "12332") jsonData.updateNumber(jsonPath: "transMode", numericStr: "1") jsonData.updateString(jsonPath: "transDistance", value: "656") jsonData.updateString(jsonPath: "transDocDate", value: "10/04/2018") jsonData.updateString(jsonPath: "vehicleNo", value: "PBN4567") jsonData.updateString(jsonPath: "vehicleType", value: "R") jsonData.i = 0 jsonData.updateString(jsonPath: "itemList[i].productName", value: "Wheat") jsonData.updateString(jsonPath: "itemList[i].productDesc", value: "Wheat") jsonData.updateNumber(jsonPath: "itemList[i].hsnCode", numericStr: "1001") jsonData.updateNumber(jsonPath: "itemList[i].quantity", numericStr: "4") jsonData.updateString(jsonPath: "itemList[i].qtyUnit", value: "BOX") jsonData.updateNumber(jsonPath: "itemList[i].cgstRate", numericStr: "0") jsonData.updateNumber(jsonPath: "itemList[i].sgstRate", numericStr: "0") jsonData.updateNumber(jsonPath: "itemList[i].igstRate", numericStr: "3") jsonData.updateNumber(jsonPath: "itemList[i].cessRate", numericStr: "4") jsonData.updateNumber(jsonPath: "itemList[i].cessAdvol", numericStr: "0") jsonData.updateNumber(jsonPath: "itemList[i].taxableAmount", numericStr: "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 jsonRequestBody = CkoJsonObject()! jsonRequestBody.updateString(jsonPath: "action", value: "GENEWAYBILL") // Setup the encryptor using the decryptedSek from the jsonAuth let crypt = CkoCrypt2()! crypt.cryptAlgorithm = "aes" crypt.cipherMode = "ecb" crypt.keyLength = 256 crypt.setEncodedKey(keyStr: jsonAuth.string(of: "decryptedSek"), encoding: "base64") crypt.encodingMode = "base64" // Encrypt the jsonData and add it to our JSON request body jsonRequestBody.updateString(jsonPath: "data", value: crypt.encryptStringENC(str: jsonData.emit())) let http = CkoHttp()! // Add the authtoken to the request header. // Be careful to be precise with uppercase/lowercase ("authtoken" vs "authToken") http.setRequestHeader(name: "authtoken", value: jsonAuth.string(of: "authToken")) http.setRequestHeader(name: "Gstin", value: "09ABDC24212B1FK") http.accept = "application/json" // POST the request to generate an e-way bill: let resp = CkoHttpResponse()! success = http.httpJson(verb: "POST", url: "http://ewb.wepgst.com/api/EWayBill", json: jsonRequestBody, contentType: "application/json", response: resp) if success == false { print("\(http.lastErrorText!)") return } var respStatusCode: Int = resp.statusCode.intValue print("response status code =\(respStatusCode)") print("response body:") print("\(resp.bodyStr!)") if respStatusCode != 200 { print("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 = CkoJsonObject()! json.load(json: resp.bodyStr) var status: Int = json.int(of: "status").intValue print("status = \(status)") if status != 1 { // Failed. Base64 decode the error // {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="} // For an invalid password, the error is: {"errorCodes":"108"} let sbError = CkoStringBuilder()! json.string(ofSb: "error", sb: sbError) sbError.decode(encoding: "base64", charset: "utf-8") print("error: \(sbError.getAsString()!)") return } json.emitCompact = false print("JSON response:") print("\(json.emit()!)") let bdData = CkoBinData()! bdData.appendEncoded(encData: json.string(of: "data"), encoding: "base64") crypt.decryptBd(bd: bdData) // Decrypts to // {"ewayBillNo":331001121234,"ewayBillDate":"24/05/2018 04:38:00 PM","validUpto":"31/05/2018 11:59:00 PM"} let jsonBill = CkoJsonObject()! jsonBill.load(json: bdData.getString(charset: "utf-8")) var ewayBillNo: Int = jsonBill.int(of: "ewayBillNo").intValue print("ewayBillNo = \(ewayBillNo)") var ewayBillDate: String? = jsonBill.string(of: "ewayBillDate") print("ewayBillDate = \(ewayBillDate!)") var validUpto: String? = jsonBill.string(of: "validUpto") print("validUpto = \(validUpto!)") // Sample output: // ewayBillNo = 331001121234 // ewayBillDate = 24/05/2018 04:55:00 PM // validUpto = 31/05/2018 11:59:00 PM } |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.