Image Uploading to Traccar Server

Furqan Mughal 6 months ago

Hey, I want to add feature for user's to upload image in our app. We want to know how Traccar Server handle's media (image) uploading to it's own server so that i save uploaded image url into my user's -> attribute param.
I have seen previous image related questions but can't find solution or answer.
Help me out!
Thanks in advance.

Furqan Mughal 6 months ago

Post Api - URL: https://x912408e9.traccar.com/api/media
Code:

Future<String?> uploadImageToTraccar({
    required File file,
    required String baseUrl,
    required String sessionId,
  }) async {
    try {
      var uri = Uri.parse("$baseUrl/api/media");

      var request = http.MultipartRequest("POST", uri)
        ..headers['Cookie'] = "JSESSIONID=$sessionId"
        ..files.add(await http.MultipartFile.fromPath("image_name", file.path));

      var response = await request.send();

      if (response.statusCode == 200) {
        var respStr = await response.stream.bytesToString();
        var jsonResp = jsonDecode(respStr);

        var mediaId = jsonResp['id'];
        if (mediaId != null) {
          return "$baseUrl/api/media/$mediaId/image_name";
        }
      } else {
        print("Upload failed: ${response.statusCode}");
      }
    } catch (e) {
      print("Error uploading image: $e");
    }
    return null;
  }
Furqan Mughal 5 months ago

Anyone for Help ???

Anton Tananaev 5 months ago

You should compare your requests to what the official web app sends.

Furqan Mughal 5 months ago

We can upload image or not ?

Anton Tananaev 5 months ago

Yes, you can.

Furqan Mughal 5 months ago

Thanks,
If i am handling a separate list under users-> attributes as json string.
I want to add image for each list item on Traccar Server and then save my list as json into attributes.
Can we do that too?
I am just asking if it is possible or not?

Anton Tananaev 5 months ago

You can't upload to user attributes. That doesn't make sense. You can upload an image in general and then add a URL to the attributes.

Furqan Mughal 5 months ago

OK Thanks for your help!

Furqan Mughal 5 months ago

Do i have to enable something on Traccar Server ?
like permission or anything before start upload image to get it's url

For Example:
traccar.xml
update this <entry key='media.readonly'>true</entry> or any thing ???

Anton Tananaev 5 months ago

No.

Furqan Mughal 5 months ago
I/flutter (30433): Upload Status: 302
I/flutter (30433): Redirect location: http://x912408e9.traccar.com/api/media/
I/flutter (30433): Redirected response status: 401
I/flutter (30433): Redirected response body: {
I/flutter (30433): "servlet":"org.eclipse.jetty.servlet.DefaultServlet-2ade8a70",
I/flutter (30433): "message":"Unauthorized",
I/flutter (30433): "url":"/api/media/",
I/flutter (30433): "status":"401"
I/flutter (30433): }
I/flutter (30433): Upload failed with status: 302
I/flutter (30433): ❌ Upload failed

Getting this type of response,
Can you please guide me which general api i have to use to upload image in general to use it's url ?

Anton Tananaev 5 months ago

Have you compared your request with what the official app sends?

Furqan Mughal 5 months ago

which request i have to see in iOS code of Traccar Manager ?

Furqan Mughal 5 months ago

i have downloaded the source code from here
https://github.com/traccar/traccar-manager

see it's correct ?