Having Trouble while creating new users on Traccar Server

Furqan Mughal6 days ago

I have an issue regarding "Create New Users".
I am trying to create new users using /users api endpoints and getting error. It says i have to send JSessionID in Api Header. But as i am trying to create new user for my Application so that user after creating it's account and use Traccar server according to our Application features. But Traccar Server need JSessionId of Admin Account which i don't get it. If I have to add my Admin Email and Password in my App and every time user's trying to create new account first i have to use admin credentials to get JSessionID and then request of new user isn't it wrong?
Help me out with better solution so that i can add scenario in my Application using Traccar Server in which i don't have to need admin JSessionID or have to login admin first to create new user account.

Anton Tananaev6 days ago

Who says that you need JSessionID?

Furqan Mughal5 days ago

Because i have tried it my self. I was trying to create user Post /users api. In response its keep sending error. And when i had added JSessionID in header my user api works perfectly also my user is created. Then i have searched on Google and ChatGPT they says we have to send JSessionID in header or otherwise we have to update server code to make create user api public.

Furqan Mughal5 days ago
Future<TraccarUser?> createUser(TraccarUser user) async {
    String url = '${Constants.instance.baseURL}users';
    final dio = Dio();

    try {
      final response = await dio.post(
        url,
        data: user.toJson(),
        options: Options(
          headers: {
            'Content-Type': 'application/json',
            'Cookie': 'JSESSIONID=${Constants.instance.sessionID}',
          },
        ),
      );

      if (response.statusCode == 200 || response.statusCode == 201) {
        debugPrint("✅ User created successfully");
        return TraccarUser.fromJson(response.data);
      } else {
        debugPrint("❌ Failed to create user: ${response.statusCode}");
        return null;
      }
    } catch (e) {
      if (e is DioError) {
        debugPrint("❌ DioError: ${e.response?.data ?? e.message}");
      } else {
        debugPrint("❌ Unexpected error: $e");
      }
      return null;
    }
  }

This is my code, when i don't send JSessionID in header api didn't work.

'Cookie': 'JSESSIONID=${Constants.instance.sessionID}',

when i send JSessionID of my main account which i used to login on my Traccar Server https://x912408e9.traccar.com/ it send success response.

Anton Tananaev5 days ago

It looks like registration is not enabled on your server. That's why it requires a session.

Furqan Mughal5 days ago

How can I enable registration ?
From code or from settings on Traccar Server web portal ?

Anton Tananaev5 days ago

Setting > Server > Registration

Furqan Mughal5 days ago

I have enabled Registration there was a check box which previously unchecked now i checked it.
And try again api and got this error. Now i have removed JSessionID from Header.
Response:
Error: Unexpected character

Furqan Mughal5 days ago

Body of api:

{id: 0, name: John Doe 2, email: ***@example.com, phone: ***, readonly: false, administrator: false, map: osm, latitude: 0.0, longitude: 0.0, zoom: 12, password: ****, coordinateFormat: dd, disabled: false, expirationTime: 2025-12-31T23:59:59Z, deviceLimit: -1, userLimit: 0, deviceReadonly: false, limitCommands: false, fixedEmail: false, poiLayer: , attributes: {}}
Anton Tananaev5 days ago

What you provided is not a valid JSON.

Also when you register a new user, only provide name, email and password. Do not send the rest.

Furqan Mughal5 days ago

Great! Now it worked well as you said only name, email & password while creating new user.

Thanks !