how to pass a param to jsonrpc http request in flutter?

Hi,
I have an login form with two textfield(email and password) and one hidden field which has db name.
I want to pass email,password,db to call odoo api for post request. While calling the odoo api using the three param I get error.

code snippet:

static var uri = "http://flutter.odoo-university.com";

 static BaseOptions options = BaseOptions(
  baseUrl: uri,
  responseType: ResponseType.plain,
  connectTimeout: 30000,
  receiveTimeout: 30000,
  validateStatus: (code) {
    if (code >= 200) {
      return true;
    }
  });
static Dio dio = Dio(options);

GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

TextEditingController _loginController = TextEditingController();

TextEditingController _passwordController = TextEditingController();

TextEditingController _databaseController = TextEditingController(text: "testodoo");

bool _isLoading = false;
Future<dynamic> _userLogin(String login, String password,String db) async

   {
      try {
    Options options = Options(
     headers: {"Content-Type": "application/json"},
    );
   var body= {
    "jsonrpc": "2.0 ",
    "params":{
     "login": login,
     "password": password,
     "db":db
      }
    };

   var json_body = {
   "jsonrpc": "2.0",
   "params": {
           "login": "nirmalasudhirr@gmail.com",
           "password": "Husband@15",
          "db": "testodoo",
     }
    };


  Response response = await dio.post('/auth/',options: options, data: json_body);

  if (response.statusCode == 200 || response.statusCode == 201) {
    var responseJson = json.decode(response.data);

    return responseJson;
   } else if (response.statusCode == 401) {
    throw Exception("Incorrect Email/Password");
  } else
    throw Exception('Authentication Error');
   } on DioError catch (exception) {
    if (exception == null ||
      exception.toString().contains('SocketException')) {
       throw Exception("Network Error");
   } else if (exception.type == DioErrorType.RECEIVE_TIMEOUT ||
      exception.type == DioErrorType.CONNECT_TIMEOUT) {
    throw Exception(
        "Could'nt connect, please ensure you have a stable network.");
    } else {
    return null;
  }
}

}

When I pass the login credentials as String(within the quotes) it works.I mean passing the value (the one in json_body) is working. I mean this part

var json_body = {
       "jsonrpc": "2.0",
          "params": {
               "login": "nirmalasudhirr@gmail.com",
              "password": "Husband@15",
             "db": "testodoo",
         }
       };

    Response response = await dio.post('/auth/',options: options, data: json_body);

the above part is working perfectly.
But when I try to pass the parameters like this

var body= {
    "jsonrpc": "2.0",
   "params":{
     "login": login,
     "password": password,
     "db":db
   }
};
  Response response = await dio.post('/auth/',data: body, options: options);

throws error.

I just don’t understand why is not working when its passed as param. Any help and explanation in this please.I would really appreciate your help.

thanks

1 Like

Hey @nirmalasudhirr, I don’t have any experience with Flutter but the issue is with the variables in the JSON file your using. It seems you would want to do something like this: https://stackoverflow.com/questions/48164223/how-to-pass-variables-from-json-to-postman-body to embed the variables into the JSON body.