How to send HTTP Post request in Tally Prime

Teja Varma
4 min readFeb 4, 2021

Here I will show you how to send HTTP POST from the tally prime.

We need :

  1. Web Service (to accept and process the request)

2. DataBase (to store data)

3. Browser (to display the data)

We will send a post request from the tally prime contains all the ledgers in the form of JSON.

Then we will accept the request and parse the JSON body and save them in SQLite3 Database.

I also created two other pages for displaying ledgers

Below is the JSON schema which we will send from tally prime

JSON schema — Tally prime

JSON response from the web service:

JSON response — web server

For web service I used the python-flask framework, you can use any other language of your choice

For the database, I used the SQlite3 database with python

Rest API:

POST - {Base url}/Ledgers/Add 

Dynamic Pages:

GET - {Base url}/Ledgers/List
GET - {Base url}/Ledgers/<string:name>

Dynamic pages are used to display the data, either you can view all the ledgers or get one single ledger by passing the name to the URL as shown above.

Here is the project structure for web service - python (Flask)

TDL Code:

[Collection:LedMasterColl]
Type:Ledger
Fetch:Name,Parent,Address,LedStateName,CountryofResidence,PartyGSTIN

[Collection:LedMastersPost]
Data Source:HTTP JSON:"http://127.0.0.1:5000/Ledgers/Add"
RemoteRequest:LedMasters:UTF8


[#Menu:GatewayOfTally]
Add:Key Item:PostLedgers:P:Call:PostLedgers

[Function:PostLedgers]
0:Walk Collection:LedMastersPost
2:Msg Box:(IF $Status then "Success" else "Failed"):$Message
3:End Walk


[Report:LedMasters]
Form:LedMsaters
Plain JSON:Yes

[Form:LedMsaters]
Part:LedMsaters
JSONTag:"DATA"

[Part:LedMsaters]
Scrolled:Vertical
Line:LedMsaters
Repeat:LedMsaters:LedMasterColl

[Line:LedMsaters]
JSONTag:"LEDGER"
Fields:Ledmst1,Ledmst2,Ledmst3,Ledmst4,Ledmst5,Ledmst6

[Field:Ledmst1]
Set as:$Name
JSONTag:"NAME"

[Field:Ledmst2]
Set as:$Parent
JSONTag:"PARENT"

[Field:Ledmst3]
Set as:$Address
JSONTag:"ADDRESS"

[Field:Ledmst4]
Set as:$LedStateName
JSONTag:"STATE"

[Field:Ledmst5]
Set as:$CountryOfResidence
JSONTag:"COUNTRY"

[Field:Ledmst6]
Set as:$PartyGSTIN
JSONTag:"GSTIN"

Python — web service (main.py)

python packages required:

  1. Flask
  2. SQLite 3 (built-in)
import sqlite3
from flask import Flask, request, jsonify, render_template

app = Flask(__name__)


@app.route('/')
def home():
return "<H1>Welcome</H1>"


@app.route('/Ledgers/List')
def ledgers():
db = get_db()
db.row_factory = sqlite3.Row
cur = db.cursor()
cur.execute("select * from Ledgers")
rows = cur.fetchall()
cur.close()
db.close()
if len(rows) > 0:
return render_template("Ledgers.html", data=rows)
else:
return "<center><H3>No data found</H3></center>"


@app.route('/Ledgers/<string:name>', methods=['GET'])
def get_ledger(name):
db = get_db()
db.row_factory = sqlite3.Row
cur = db.cursor()
cur.execute("select * from Ledgers where name = ?", [name])
row = cur.fetchone()
cur.close()
db.close()
if row:
return render_template("Ledger.html", led=row)
else:
return "<center><H3>No data found</H3></center>"


@app.route('/Ledgers/Add', methods=['POST'])
def add_ledgers():
data = request.get_json()
db = get_db()
try:
for led in data['DATA']['LEDGER']:
name = led["NAME"]
group = led["PARENT"]
address = led["ADDRESS"] if "ADDRESS" in led else "NIL"
state = led["STATE"] if "STATE" in led else "NIL"
country = led["COUNTRY"] if "COUNTRY" in led else "NIL"
gst = led["GSTIN"] if "GSTIN" in led else "NIL"
db.execute("INSERT INTO Ledgers (name,parent,address,state,country,gstin) VALUES(?,?,?,?,?,?)",
(name, group, address, state, country, gst))
db.commit()
except Exception as e:
print(e)
return jsonify(status=False, message=str(e))
finally:
db.close()
return jsonify(status=True, message="Ledgers inserted successfully")


def get_db():
db = sqlite3.connect("ledgers.db")
db.execute(
"CREATE TABLE IF NOT EXISTS Ledgers (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,parent TEXT,address TEXT,state TEXT,country TEXT,gstin TEXT)")
db.commit()
return db


if __name__ == "__main__":
app.run(debug=True)

Dynamic web page — Getting the list of ledgers:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>List Of Ledgers</title>
<style>

body{
font-family:sans-serif
}
h2{
color:tomato;
text-decoration:underline
}
table{
width:100%
}
table,th,td{
border: 1px solid black;
border-collapse: collapse;
border-spacing: 5px;
}
th, td {
padding: 15px;
}
th{
color:white;
background-color:green
}
td{
background-color:rgba(255, 99, 71, 0.5);
font-weight:bold
}


</style>
</head>
<body>
<center><H2>LEDGERS</H2></center>
<table>
<tr>
<th>NAME</th>
<th>GROUP</th>
<th>ADDRESS</th>
<th>STATE</th>
<th>COUNTRY</th>
<th>GSTIN</th>
</tr>
{% for led in data %}
<tr>
<td>{{led["name"]}}</td>
<td>{{led["parent"]}}</td>
<td>{{led["address"]}}</td>
<td>{{led["state"]}}</td>
<td>{{led["country"]}}</td>
<td>{{led["gstin"]}}</td>
</tr>
{% endfor %}
</table>
</body>
</html>

Dynamic web page — for getting Ledger by name

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ led["name"] }}</title>
<style>

body{
font-family:sans-serif
}
h3{
color:tomato;
}
table{
width:100%
}
table,td{
border: 1px solid black;
border-collapse: collapse;
border-spacing: 5px;
}
td {
padding: 15px;
}
td{
background-color:rgba(255, 99, 71, 0.5);
font-weight:bold
}



</style>
</head>
<body>
<H3>(Ledger - {{ led["name"] }})</H3>
<table>
<tr>
<td>NAME</td>
<td>{{ led["name"] }}</td>
</tr>
<tr>
<td>GROUP</td>
<td>{{ led["parent"] }}</td>
</tr>
<tr>
<td>ADDRESS</td>
<td>{{ led["address"] }}</td>
</tr>
<tr>
<td>STATE</td>
<td>{{ led["state"] }}</td>
</tr>
<tr>
<td>COUNTRY</td>
<td>{{ led["country"] }}</td>
</tr>
<tr>
<td>GSTIN</td>
<td>{{ led["gstin"] }}</td>
</tr>
</table>
</body>
</html>

Steps to run it

  1. Load the TDL Code in Tally Prime
  2. Run python file -main.py
  3. Open Tally Prime and click on Post Ledgers option in GatewayOfTally
  4. Then open browser and goto -> localhost:5000/Ledgers/List
  5. For getting single ledger goto -> localhost:5000/Ledgers/<name>

Check out my GitHub repo for stuff like this: https://github.com/tejavarma-aln

Follow me on

Facebook: https://www.facebook.com/Softcdc

Telegram: https://t.me/tdlcommunity

Thank you!

--

--