-1

I am having error, kindly help me with this code, new to Flutter here. When I'm using https://jsonplaceholder.typicode.com/posts it is working fine, but I am receiving error using my company URL, but when I am using postman to POST the company URL it is working fine, kindly refer to my code and the image below.

Code:

   Dio dio = Dio();

  Future postData() async{
    const String pathUrl = 'https://202.57.135.121:8443/icbs/ATMInterfaceListener/mobileAppRegistration';
    //const String pathUrl = 'https://jsonplaceholder.typicode.com/posts';

dynamic data = {
  'email': 'darwikf@gkdkkk.com',
  'password': 'test123456',
  'firstName': 'Darwin',
  'middleName': 'D',
  'lastName': 'L',
  'birthDate': 095545,
  'mobile': 1256789,
  'telephone': 12367891,
  'presentAddress': 'mla',
  'permanentAddress': 'mla',
  'country': 'phl',

  // 'title': 'Darwin',
  // 'body': 'Success',
  // 'userId': '1',
};

try{
  var response = await dio.post(pathUrl, data: data, options: Options(
    followRedirects: false,
    validateStatus: (status) => true,
    headers: {
      'Content-type' : 'application/json; charset=UTF-8',
    }
  ));
  return response.data;
}catch(e){
  print(e);
}

}`

Image Postman Postman

Image Error HTML response

DL Studio
  • 267
  • 3
  • 7

2 Answers2

2

I suspect you're running into CORS issues, perhaps because you're trying to connect to a host that isn't the host where the flutter code was being loaded from. CORS protects you, but it sometimes is hard to avoid. There are solutions... google "flutter cors" and see which ones can work best for your situation.

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70
0

This is the exact answer, this works for me.

Dio dio = Dio();
    
Future postData() async{
    const String pathUrl = 'https://202.57.135.121:8443/icbs/ATMInterfaceListener/mobileAppRegistration';
    dynamic data = {
      'email': 'darwikf@gkdkkk.com',
      'password': 'test123456',
      'firstName': 'Darwin',
      'middleName': 'D',
      'lastName': 'L',
      'birthDate': 095545,
      'mobile': 1256789,
      'telephone': 12367891,
      'presentAddress': 'mla',
      'permanentAddress': 'mla',
      'country': 'phl',
    };
    try{
      var response = await dio.post(pathUrl, data: data, options: Options(
          followRedirects: false,
          validateStatus: (status) => true,
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
          }
      ));
      return response.data;
    }catch(e){
      print(e);
    }
}
DL Studio
  • 267
  • 3
  • 7