getting TypeError: can't concat str to bytes error while executing historic api . plz help


  • TypeError Traceback (most recent call last)
    <ipython-input-11-bab6b51f7b98> in <module>
    23 'Content-Type': 'application/json'
    24 }
    ---> 25 conn.request("POST", "/rest/secure/angelbroking/historical/v1/getCandleData", payload, headers)
    26 res = conn.getresponse()
    27 data = res.read()

    ~\anaconda3\lib\http\client.py in request(self, method, url, body, headers, encode_chunked)
    1250 encode_chunked=False):
    1251 """Send a complete request to the server."""
    -> 1252 self._send_request(method, url, body, headers, encode_chunked)
    1253
    1254 def _send_request(self, method, url, body, headers, encode_chunked):

    ~\anaconda3\lib\http\client.py in _send_request(self, method, url, body, headers, encode_chunked)
    1296 # default charset of iso-8859-1.
    1297 body = _encode(body, 'body')
    -> 1298 self.endheaders(body, encode_chunked=encode_chunked)
    1299
    1300 def getresponse(self):

    ~\anaconda3\lib\http\client.py in endheaders(self, message_body, encode_chunked)
    1245 else:
    1246 raise CannotSendHeader()
    -> 1247 self._send_output(message_body, encode_chunked=encode_chunked)
    1248
    1249 def request(self, method, url, body=None, headers={}, *,

    ~\anaconda3\lib\http\client.py in _send_output(self, message_body, encode_chunked)
    1062 # chunked encoding
    1063 chunk = f'{len(chunk):X}\r\n'.encode('ascii') + chunk
    -> 1064 + b'\r\n'
    1065 self.send(chunk)
    1066

    TypeError: can't concat str to bytes


  • HI @puna123 Its a user specific requirement you need to connect with your software development team.


  • while executing below code. i am getting above error.

    import http.client
    conn = http.client.HTTPSConnection("apiconnect.angelbroking.com")
    payload = {
    "exchange": "NSE ",
    "symboltoken ": "3045 ",
    "interval ": "ONE_MINUTE ",
    "fromdate ": "2021 - 02 - 10 09: 15 ",
    "todate ": "2021 - 02 - 10 09: 16 "
    }
    #"{"exchange": "NSE","symboltoken": "3045","interval": "ONE_MINUTE","fromdate": "2021-02-10 09:15","todate": "2021-02-10 09:16"}"
    headers = {
    'X-PrivateKey': '********',
    'Accept': 'application/json',
    'X-SourceID': 'WEB',
    'X-ClientLocalIP': 'CLIENT_LOCAL_IP',
    'X-ClientPublicIP': 'CLIENT_PUBLIC_IP',
    'X-MACAddress': 'MAC_ADDRESS',
    'X-UserType': 'USER',
    'Authorization': 'Bearer AUTHORIZATION_TOKEN',
    'Accept': 'application/json',
    'X-SourceID': 'WEB',
    'Content-Type': 'application/json'
    }
    conn.request("POST", "/rest/secure/angelbroking/historical/v1/getCandleData", payload, headers)
    res = conn.getresponse()
    data = res.read()
    print(data.decode("utf-8"))