KiteConnect postback example

tonystark
Hi traders,

Here is a simple generic example for implementing a postback mechanism. You can host this in a remote server or in your local server itself.

https://gist.github.com/ajinasokan/267d68c9f61e3e4ea11681c0ec4e707d
  • Shaha
    Thanks. Will definately try this. You saved my lot of time.
  • urrakesh2053
    Hi @tonystark

    I have already followed above link step by step to setup postback. Everything looks fine but somehow script "app.py" is not throughing out results in its log. log file is totally empty throughout (even if n numbers of orders executed).

    I can see POST/GET lines entries at ngrok online console.



    but I don't know how to take these data into my main python script. I am having tough time in fetching json data into my python script.

    any other reference ??

    Thanks,
  • tonystark
    tonystark edited December 2017
    You should give the post back URL as https://yourdomain/post
  • urrakesh2053
    Hi @tonystark ,

    Right now I have given https://95b6a468.ngrok.io as postback url at my kite app dashboard.
    Are you suggesting that I should use https://95b6a468.ngrok.io/post ?

    following is the screenshot of my kite app dashboard

  • tonystark
    tonystark edited December 2017
    Yes
  • urrakesh2053
    tried that... got following error

    127.0.0.1 - - [27/Dec/2017 13:43:45] "POST /post HTTP/1.1" 500 -
    Traceback (most recent call last):
    File "/home/rakesh/anaconda3/lib/python3.4/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
    File "/home/rakesh/anaconda3/lib/python3.4/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
    File "/home/rakesh/anaconda3/lib/python3.4/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
    File "/home/rakesh/anaconda3/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
    raise value
    File "/home/rakesh/anaconda3/lib/python3.4/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
    File "/home/rakesh/anaconda3/lib/python3.4/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
    File "/home/rakesh/anaconda3/lib/python3.4/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
    File "/home/rakesh/anaconda3/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
    raise value
    File "/home/rakesh/anaconda3/lib/python3.4/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
    File "/home/rakesh/anaconda3/lib/python3.4/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
    File "/home/rakesh/wksp1/my_trade/GoogleFin/Kite_new/app.py", line 69, in post
    f.write(request.get_data() + '\n')
    TypeError: can't concat bytes to str
  • tonystark
    Can you try changing the line:

    f.write(request.get_data() + '\n')

    to
    f.write(request.get_data())
    f.write('\n')
  • urrakesh2053
    urrakesh2053 edited December 2017
    Found the resolution. :)

    Following correction is working fine now.

    f.write(str(request.get_data())+'\n')

    Thanks for the reply.

    So, Idea is any placed order gets executed, there will be a new post appended in log file. This is working totally fine now. However, i have to keep on polling this log file to detect any change in log file and read last line through my script. I can google it how to do it in python, but do you have any piece of reliable (best way) code to do that. Kindly share that too.

    Thanks a lot again.
  • tonystark
    You can actually integrate that python script into yours. Instead of writing it into a file you can call another function with that request data for further processing. The above gist can be reduced to:
    from flask import Flask, request
    app = Flask(__name__)

    @app.route('/post', methods=['POST'])
    def post():
    yourfunction(str(request.get_data()))
    return 'done'

    app.run(debug=True, host='0.0.0.0', port=80)
  • jjiteshh
    jjiteshh edited November 2018
    Hey guys,

    I am trying to set up the postback url as mentioned here. I get an error 404 not found and the request is not POST its coming as HEAD.

    I have set up exactly like above using ngrok and flask app. I am able receive response made from other POST requests.




    I am suspecting this might not be working as the I am trying this after market hours. I will try this again tomorrow morning.
  • sujith
    sujith edited November 2018
    The HEAD request is made to validate the URL when a user changes it on the developers console.
  • jjiteshh
    @sujith

    yes as soon as i update the link on the console i get this message.

    But i am not able to receive any messages related to orders. I have tried using postman to send json data and it works from that end.
  • jjiteshh
    never mind,

    I received the data when the order got cancelled. I was expecting a message when the order was placed. But this is okay.

    Thanks
This discussion has been closed.