KiteConnect quote raises "Unknown content-type" data exception after succeeding a few times

dhwanit
dhwanit edited January 2021 in Python client
My code is trying to fetch quotes for the options universe on all NFO instruments once every 5 minutes (~41000 trading symbols). The list of instruments (trading symbols) is parsed from a downloaded CSV that's fetched once a day from https://api.kite.trade/instruments/NFO. The code is part of a larger Django project that tracks these instruments every 5 minutes via a celery background task.

The code calls kite.quote multiple times in a loop, fetching 500 instruments at a time as limited per the API docs. The code also waits 2 seconds in-between each call to kite.quote to ensure that the app doesn't breach the limit of 1 request / second for obtaining quotes (I found an answer in another forum discussion indicating the 1r / s limitation). The expectation is that it will take about 2.5-3 minutes to fetch quotes for all NFO instruments using this loop.

However, when run, it succeeds a few times (usually 3-4), fetching about 1500-2000 quotes over 10-15 seconds before failing with the exception below:


raise ex.DataException("Unknown Content-Type ({content_type}) with response: ({content})".format(
kiteconnect.exceptions.DataException: Unknown Content-Type (text/html) with response: (b'<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n')


My dev environment is running: Python 3.8.6 with KiteConnect 3.9.0

Are there any other API limitations/restrictions, or KiteConnect config that I have to be aware of? Anyone else faced a similar issue before? Any inputs would be appreciated to help resolve this issue. Thanks in advance!

The full exception traceback is provided below:


Traceback (most recent call last):
File "manage.py", line 25, in <module>
main()
File "manage.py", line 21, in main
execute_from_command_line(sys.argv)
File "/Users/dhwanit/.pyenv/versions/3.8.6/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/Users/dhwanit/.pyenv/versions/3.8.6/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/dhwanit/.pyenv/versions/3.8.6/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/dhwanit/.pyenv/versions/3.8.6/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/Users/dhwanit/.pyenv/versions/3.8.6/lib/python3.8/site-packages/django/core/management/commands/shell.py", line 93, in handle
exec(sys.stdin.read())
File "<string>", line 4, in <module>
File "/Users/dhwanit/projects/marketbots/tracker/libs/tracker.py", line 220, in nfo_universe
quotes = kw.get_quote(instrument_subset_list)
File "/Users/dhwanit/projects/marketbots/tracker/libs/kitewrapper.py", line 33, in get_quote
quotes = kite.quote(instruments)
File "/Users/dhwanit/.pyenv/versions/3.8.6/lib/python3.8/site-packages/kiteconnect/connect.py", line 570, in quote
data = self._get("market.quote", params={"i": ins})
File "/Users/dhwanit/.pyenv/versions/3.8.6/lib/python3.8/site-packages/kiteconnect/connect.py", line 822, in _get
return self._request(route, "GET", url_args=url_args, params=params, is_json=is_json)
File "/Users/dhwanit/.pyenv/versions/3.8.6/lib/python3.8/site-packages/kiteconnect/connect.py", line 900, in _request
raise ex.DataException("Unknown Content-Type ({content_type}) with response: ({content})".format(
kiteconnect.exceptions.DataException: Unknown Content-Type (text/html) with response: (b'<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n')
  • sujith
    I think you might be exceeding the URL length limit.
    You will have to reduce the number of instrument tokens in one go.
  • madhumattangi
    I am trying to fetch BANKNIFTY Futures data. Still getting this error.
  • rakeshr
    Can you paste here the complete debug log as explained here?
  • sujith
    @madhumattangi,
    You need to fetch instruments master file once and save it locally. If you keep polling then Cloudflare might block you thinking these are rogue requests.
  • IO4946
    Unknown Content-Type (text/html) with response: (b'

    400 Bad request

    \nYour browser sent an invalid request.\n\n') I m getting this error and and i am not calling multiple instruments just banknifty . Plz tell how to resolve this error
  • rakeshr
    The instrument file for the day is available post-07:30 AM. You need to request and store post that.
Sign In or Register to comment.