DataException : 502 Bad Gateway

While trying to fetch last traded price of a stock using a loop which has sleep time of 3 seconds, I am getting the following error.
DataException: Unknown Content-Type (text/html) with response: (b'\r\n502 Bad Gateway\r\n\r\n

502 Bad Gateway

Following is the code used inside the loop.

# Get the current market price
scripCode ='NSE'+':'+Symbol_Buy_Order
Current_Price = kite.ltp(scripCode)[scripCode]['last_price']

Can anyone help me ?
  • Unbreakable
    I got this error today. when some of other broker's server was also down. Is it because Zerodha's server was unable to fulfill the request/server down?
  • rakeshr
    DataException: Unknown Content-Type (text/html) with response: (b'\r\n502 Bad Gateway\r\n\r\n
    Can you let us know the request time for 502? We haven't encounter any 5xx errors at our end, for yesterday market timings.
  • Unbreakable
    Today also I received a similar error. I am using the below code to find the leverage for intraday order.

    #Finding leverage
    order_param_single = [{
    "exchange": "NSE",
    "transaction_type": "BUY",
    "variety": "regular",
    "product": "MIS",
    "order_type": "MARKET",
    "quantity": 1

    margin_detail = kite.order_margins(order_param_single)
    margin_dic = margin_detail[0]
    Intraday_leverage = margin_dic["leverage"]

    I think above section of code is generating the error. Following is the error I am receiving.

    DataException Traceback (most recent call last)
    Cell In[6], line 228
    217 #Finding leverage
    218 order_param_single = [{
    219 "exchange": "NSE",
    220 "tradingsymbol":symbol_Buy_Order,
    225 "quantity": 1
    226 }]
    --> 228 margin_detail = kite.order_margins(order_param_single)
    229 margin_dic = margin_detail[0]
    230 Intraday_leverage = margin_dic["leverage"]

    File ~\anaconda3\envs\kiteconnect\Lib\site-packages\kiteconnect\, in KiteConnect.order_margins(self, params)
    769 def order_margins(self, params):
    770 """
    771 Calculate margins for requested order list considering the existing positions and open orders
    773 - `params` is list of orders to retrive margins detail
    774 """
    --> 775 return self._post("order.margins", params=params, is_json=True)

    File ~\anaconda3\envs\kiteconnect\Lib\site-packages\kiteconnect\, in KiteConnect._post(self, route, url_args, params, is_json, query_params)
    863 def _post(self, route, url_args=None, params=None, is_json=False, query_params=None):
    864 """Alias for sending a POST request."""
    --> 865 return self._request(route, "POST", url_args=url_args, params=params, is_json=is_json, query_params=query_params)

    File ~\anaconda3\envs\kiteconnect\Lib\site-packages\kiteconnect\, in KiteConnect._request(self, route, method, url_args, params, is_json, query_params)
    941 return r.content
    942 else:
    --> 943 raise ex.DataException("Unknown Content-Type ({content_type}) with response: ({content})".format(
    944 content_type=r.headers["content-type"],
    945 content=r.content))

    DataException: Unknown Content-Type (text/html) with response: (b"

    504 Gateway Time-out

    \nThe server didn't respond in time.\n\n")
  • MAG
    Are you running this on a home broadband connection or on a cloud/datacenter server like AWS etc?
    If its home broadband, its probably an issue with your broadband connection flaking.
  • Unbreakable
    yes. Using home broadband connection. Is there any way to resolve this issue?
  • Unbreakable
    Will I be able to overcome this issue by modifying the code as shown below.

    order_param_single = [{
    "exchange": "NSE",
    "tradingsymbol": symbol_Buy_Order,
    "transaction_type": "BUY",
    "variety": "regular",
    "product": "MIS",
    "order_type": "MARKET",
    "quantity": 1

    max_retries = 3
    retry_delay = 1 # seconds

    for _ in range(max_retries):
    margin_detail = kite.order_margins(order_param_single)
    margin_dic = margin_detail[0]
    Intraday_leverage = margin_dic["leverage"]
    break # If successful, exit the loop
    except :
    # This block will be executed if all retries fail
    print(f"Max retries reached. Could not get leverage.")

  • MAG
    You can try that - but there are no guarantees it will work. We have no idea whats going on with your broadband internet connection other than the fact that its flaking. And most home broadband connections are like this. They will have temp conn issues randomly at different times of the day.

    The only valid solution is to move to a cloud VPS provider like AWS/Azure. These are run in world class datacenters with reliable and redundant network and power connections so they would rarely have such issues.
    Anyone and everyone I know who trades seriously has their setup on such cloud service providers. Home broadband will never be stable enough for live trading.
Sign In or Register to comment.