Timeout command error 400 (python)

Hi
I have a chatbot with my own account and i am trying to create a timeout command. My bot has all needed scopes. For testing i want a “$v” command that will timeout the user for one second. i have tried different things like using “https://api.twitch.tv/helix/moderation/bans” as URL and other formats for the json data like:

    json_data = {
        'data': {
            'user_id': user_id,
            'duration': duration,  
            'reason': reason  
        }
    }

Here is my last attempt:

@commands.command(name='v')
async def vanish(self, ctx):
    broadcaster_id = "my_actual_ID"  
    moderator_id = "my_actual_ID"  
    user_id = ctx.author.id  
    duration = 1  
    reason = "Vanish command" 

    headers = {
        "Client-Id": os.getenv('MY_CLIENT_ID'),
        "Authorization": f "Bearer {os.getenv('MY_ACCESS_TOKEN')}",
        "Content-Type": "application/json"
    }

    url = f "https://api.twitch.tv/helix/moderation/bans?broadcaster_id={broadcaster_id}&moderator_id={broadcaster_id}"

    payload = {
        "user_id": user_id,
        "duration": duration,
        "reason": reason
    }

    response = requests.post(url, headers=headers, json=payload)
    if response.status_code == 204:
        await ctx.send(f"{ctx.author.name} is gone. Poof!")
    else:
        print(response.text)
        await ctx.send("There was a problem executing the command.")

I must be overlooking something or not understanding it enough. According to prints it sends the correct user_id but I still get the message: {“error”: “Bad Request”, “status”:400, “message”: “Missing required parameter "user_id"”}
Can anyone help me?

Should be

payload = [
    {
        "user_id": user_id,
        "duration": duration,
        "reason": reason
    }
]

as in you send an array of one object to ban a user

Hey Barry
Thank you for your answer. Unfortunately, I now get the following message: {“error”: “Bad Request”, “status”:400, “message”: “Request body was not parsable. Attempted Content-Type: "application/json"”}

I’m not sure if my posted code is correct for the constuction of a JSON payload for python that sends a JSON blob with an array of one object.

So it is likely that and/or python not consturcting the JSON blob correctly

Thank you Barry!

now it is working:

    json_data = {
        'data': {
            'user_id': user_id,
            'duration': duration,  
            'reason': reason  
        }
    }
    json_payload = json.dumps(json_data)
    response = requests.post(url, headers=headers, data=json_payload)

    if response.status_code == 200:

ah yeah. I missed the data key :smiley: my brain fart!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.