Getting a 'bytes-like object required' error when using a Discord bot with Python

I’m trying to build a Discord bot in Python that logs messages and sends them to a service called Hologram.io. I rewrote my code to handle the logging and HTTP request, but when I run it, it throws an error that says:

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

I’ve attempted to modify how the JSON payload is formatted, including different adjustments with the API key, but nothing resolves the issue. Below is a revised version of my code snippet:

import urllib.request
import discord

client = discord.Client()

@client.event
async def on_message(message):
    body = f"[{message.timestamp:%H:%M}] {message.author}: {message.content}"
    values = f'''{{
      "deviceid": {12345},
      "fromnumber": "{'+1-000-000-0001'}",
      "body": "{body}"
    }}'''  
    request = urllib.request.Request(
        f'https://api.example.com/sms?key={'your_api_key'}', 
        data=values, 
        headers={'Content-Type': 'application/json'}
    )
    response = urllib.request.urlopen(request).read()

client.run('email', 'password')

What could be causing this error and how can I properly convert my data to a bytes-like object before sending the HTTP request?

I faced a similar problem before. The error is caused by providing a string when the function expects bytes. In my case, the solution was to construct the JSON payload as a proper dictionary, convert it to a JSON string with json.dumps(), and then encode that string in UTF-8 before making the request. This ensures that the data is in the correct bytes-like format for the HTTP request and matches the specified Content-Type. The approach helped eliminate the error related to data type mismatch.

hey josephk, i ran into a similar issue. try encoding ur data like this:

data = values.encode(‘utf-8’)

and use that in ur request. the error happens cuz urllib expects bytes not a string. hope this helps! lmk if u need more info