# Make RPC call from Flutter and dio

**URL:** <https://discuss.tryton.org/t/make-rpc-call-from-flutter-and-dio/4349>\
**Category:** Developer\
**Created:** [June 23, 2021, 4:05pm UTC](https://discuss.tryton.org/t/make-rpc-call-from-flutter-and-dio/4349 "2021-06-23T16:05:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DaZD](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/dazd/32/1502_2.png) [@DaZD](https://discuss.tryton.org/u/DaZD)\
**Post date:** [June 23, 2021, 4:05pm UTC](https://discuss.tryton.org/t/make-rpc-call-from-flutter-and-dio/4349/1 "2021-06-23T16:05:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DaZD](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/dazd/32/1502_2.png) [@DaZD](https://discuss.tryton.org/u/DaZD)\
**Post date:** [June 24, 2021, 3:03pm UTC](https://discuss.tryton.org/t/make-rpc-call-from-flutter-and-dio/4349/2 "2021-06-24T15:03:49Z")

</div>

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

```auto
<!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 :

```auto
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.

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [June 24, 2021, 3:13pm UTC](https://discuss.tryton.org/t/make-rpc-call-from-flutter-and-dio/4349/3 "2021-06-24T15:13:34Z")

</div>

> [@DaZD](#):
>
> `var url_dio = "http://192.168.1.17:8000/tryton";`

You must add a trailing `/` to the URL.

---

<div class="post-metadata">

**Author:** ![DaZD](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/dazd/32/1502_2.png) [@DaZD](https://discuss.tryton.org/u/DaZD)\
**Post date:** [June 24, 2021, 3:30pm UTC](https://discuss.tryton.org/t/make-rpc-call-from-flutter-and-dio/4349/4 "2021-06-24T15:30:57Z")

</div>

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 😉

D

---

<div class="post-metadata">

**Author:** ![DaZD](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/dazd/32/1502_2.png) [@DaZD](https://discuss.tryton.org/u/DaZD)\
**Post date:** [June 24, 2021, 6:49pm UTC](https://discuss.tryton.org/t/make-rpc-call-from-flutter-and-dio/4349/5 "2021-06-24T18:49:41Z")

</div>

Complete solution :

```auto
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);
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)\
**Post date:** [July 24, 2021, 6:50pm UTC](https://discuss.tryton.org/t/make-rpc-call-from-flutter-and-dio/4349/6 "2021-07-24T18:50:04Z")

</div>

Closed on July 24, 2021, 6:50pm UTC

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