How to create subdomain in C-Panel using flutter mobile app?

I need to create subdomains in my website using flutter mobile application. When I input the subdomain name in my app, it should be generate a subdomain in cpanel.

Future<void> createSubdomain(String subdomainName) async {
    setState(() {
      _isLoading = true; 
    });
    try {
      const cpanelUrl="http://www.website.com:2082/cpanel";
      const apiKey = 'myAPI';

      final requestBody = {
        'module': 'Subdomain',
        'function': 'create',
        'domain': 'mywebsite.com',
        'subdomain': subdomainName,
        'apiversion': '2', // Use the appropriate API version
      };

      print('subDn $subdomainName');

      const username="uname";
      const password = 'pword`your text`';
      final credentials = utf8.encode('$username:$password');
      final base64Credentials = base64Encode(credentials);

      final headers = {
        'Authorization': 'Basic $base64Credentials',
        'Authorization-Token': apiKey, // Include the API key
      };

      final response = await http.post(
        Uri.parse('$cpanelUrl/ExecCgi'),
        headers: headers,
        body: requestBody,
      );

      if (response.statusCode == 200) {
        final jsonResponse = json.decode(response.body);
        if (jsonResponse['result'] == 1) {
          setState(() {
            _responseText="Subdomain created successfully!";
          });
          print(_responseText);
        } else {
          setState(() {
            _responseText="Error: ${jsonResponse["error']}';
          });
          print(_responseText);
        }
      } else {
        setState(() {
          _responseText="Error: HTTP ${response.statusCode}";
        });
        print(_responseText);
      }
    } finally {
      setState(() {
        _isLoading = false; // Set loading to false when the process completes
      });
    }
  }

This function does not create a subdomain. No response comes. Here comes the error

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Connection timed out
E/flutter ( 9092): #0 IOClient.send (package:http/src/io_client.dart:94:7)

Leave a Comment