Make RPC call from Flutter and dio

Hey Markus,

I’m trying to connect to Tryton with Flutter via RPC using dio.
Could you please share some basic flutter/dio code to connect to Tryton and retrieve some datas ?

D

I tried 3 different ways to connect but I get a status error 301 :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>Redirecting...</title>
<h1>Redirecting...</h1>
<p>You should be redirected automatically to target URL: <a href="http://192.168.1.17:8000/tryton/">http://192.168.1.17:8000/tryton/</a>. If not click the link.

Here is my code :

Map<String, dynamic> data = {
  'params': [
    "admin",
    {"password": "myPassword"}
  ],
  'jsonrpc': "2.0",
  'method': 'common.db.login',
  'id': 1,
};

var headers = {"Content-Type": "application/json"};
var url = Uri.http('192.168.1.17:8000', "/tryton");
var url_dio = "http://192.168.1.17:8000/tryton";

void connect_to_tryton_async() async {
  var response = await http.post(url, headers: headers, body: jsonEncode(data));
  if (response.statusCode == 200) {
    print(response.body);
  } else {
    print('A network error occurred');
  }
}

void connect_to_tryton_sync() {
  http
      .post(url, headers: headers, body: json.encode(data))
      .then((response) => print(response.body))
      .catchError((error) => print(error));
}

void connect_to_tryton_dio() async {
  var dio = Dio();
  try {
    Response response = await dio.post(url_dio,
        data: json.encode(data), options: Options(headers: headers));
    print(response);
  } on DioError catch (err) {
    print(err);
  }
}

Rq : I can connect with a python script (same as Markus posted)

Obviously I’m missing something here.

You must add a trailing / to the URL.

Thank you very much @ced it works now. The answer was in the question… well I should have read the error message with more attention.
I will post the method to retrieve data as soon as I manage to make it work :wink:

D

Complete solution :

Map<String, dynamic> data = {
  'params': [
    "admin",
    {"password": "myPass"}
  ],
  'jsonrpc': "2.0",
  'method': 'common.db.login',
  'id': 1,
};

dynamic session;
int request_id = 0;
var headers = {"Content-Type": "application/json"};
var url = Uri.http('192.168.1.17:8000', "/tryton/");
var url_dio = "http://192.168.1.17:8000/tryton/";

void connect_to_tryton_dio() async {
  var dio = Dio();
  try {
    Response response = await dio.post(url_dio,
        data: json.encode(data), options: Options(headers: headers));
    session = response.data;

   // now we have a session we can call rpc functions like this : 
    call("model.res.user", "get_preferences", [[], {}]);

    // example with params :
    call("model.transport.driver", "search_read", [
      [], // domain
      0, // offset
      null, // limit
      null, // order
      ["code", "employee.rec_name"],   // fieldsNames
      {} // context 
    ]);
  } on DioError catch (err) {
    print(err);
  }
}

void call(prefix, method, params) async {
  method = prefix + "." + method;
  var payload = {
    'params': params,
    'method': method,
    'id': ++request_id,
  };
  var sess = "admin:" + session[0].toString() + ":" + session[1];
  String auth = base64Encode(ascii.encode(sess));
  auth = 'Session  ' + auth;
  headers = {'content-type': 'application/json', 'authorization': auth};
  var dio = Dio();
  try {
    Response response = await dio.post(url_dio,
        data: json.encode(payload), options: Options(headers: headers));
    print(response.data);
  } on DioError catch (err) {
    print(err);
  }
}
1 Like

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