Skip to content

Share Plex library in Dart

Hi Iā€™m figuring out how to share Plex library in dart. I’m helping myself with this working script in python ( it works)

unfortunately my code returns an http error 400, bad request

note: When I run the dart code there are no shared library.

maybe the payload is not correct šŸ˜ Thank for any help

import 'package:http/http.dart' as http;
class Server {
   String token, ip, port;
   Server(this.ip, this.port, this.token);
   share() async {
     var server_id = '00000000000000000000000000000000'; fake id
     var library_section_ids = '97430074'; // right id , it works in python
     var invited_id = '50819899'; // right id , it works in python
     var headers = {
       'Accept': 'application/json',
       'X-Plex-Token': token,
    var data =
    '["server_id": $server_id,'
    ' "shared_server":["library_section_ids":[$library_section_ids],'
    ' "invited_id":$invited_id]';
   var res = await
    headers: headers,
    body: data);
   if (res.statusCode != 200) {
     throw Exception(' error: statusCode= ${res.statusCode}');


void main(List<String> arguments) async {
  var srv = Server('', '32400', '000000000-1111');
  await srv.share();



The old code has a few bug and don’t encode perfectly the data I solved with ‘dio’ package and this link helped me a lot

If the user has no shared library this code add a new one

import 'package:dio/dio.dart';
class Server {
   String token;
   test() async {
      var serverId = '';
      var librarySectionIds = ;
      var invitedId = ;
      var dio = Dio();
      var data = {
         "server_id": serverId,
         "shared_server": {
         "library_section_ids": librarySectionIds,
         "invited_id": invitedId
    dio.options.headers['Accept'] = 'application/json';
    dio.options.headers["authorization"] = "Bearer $token";
    var response = await
       data: data);