Add user using REST API - python

Hi, I wrote a script to add user using REST API socket through python. But it always gives an error: {‘result’: {‘message’: ‘cannot create user: device already managed’}, ‘status’: ‘Bad Request’, ‘status-code’: 400, ‘type’: ‘error’}

This is how i add system user after disabling console-conf right? Could you help me with this?

#! /usr/bin/env python3
import requests
import socket
import pprint

from urllib3.connection import HTTPConnection
from urllib3.connectionpool import HTTPConnectionPool
from requests.adapters import HTTPAdapter


class SnapdConnection(HTTPConnection):
    def __init__(self):
        super().__init__("localhost")

    def connect(self):
        self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        self.sock.connect("/run/snapd.socket")


class SnapdConnectionPool(HTTPConnectionPool):
    def __init__(self):
        super().__init__("localhost")

    def _new_conn(self):
        return SnapdConnection()


class SnapdAdapter(HTTPAdapter):
    def get_connection(self, url, proxies=None):
        return SnapdConnectionPool()


session = requests.Session()
session.mount("http://snapd/", SnapdAdapter())
response = session.post("http://snapd/v2/users", json = {"action": "create", "email": "<some email>", "sudoer": True})
pprint.pprint(response.json())

Thank you

not sure, but i think you do not want “snapd” in that url …

Thanks for the reply. But i later managed to figure it out:

I had to change it to :

response = session.post("http://snapd/v2/users", json = {"action": "create", "email": "<some email>", "sudoer": True, "force-managed": True})