How to resolve Json Error: TypeError: a bytes-like object is required, not 'str'

Viewed 294

pls correction with correct syntax pythoh piece of Code that cause error:

def reliable_send(self, data):
        json_data = json.dumps(data)
        self.connection.send(json_data)

Error is:

self.connection.send(json_data) at json_data whic is

TypeError: a bytes-like object is required, not 'str'

1 Answers

you need to encode the data before sending, this should be the solution

def reliable_send(self, data):
    json_data = json.dumps(data)
    self.connection.send(json_data.encode())
Related