Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

addsubdomain API call error

Discussion in 'cPanel Developers' started by swbrains, May 24, 2018.

  1. swbrains

    swbrains Well-Known Member

    Joined:
    Sep 13, 2006
    Messages:
    127
    Likes Received:
    15
    Trophy Points:
    168
    In the past on my old server (CentOS7; cPanel 68), my Perl script called the API function addsubdomain to create a new subdomain. On my new server (CentOS7, cPanel 70.0.44), the same code returns an error from the addsubdomain API call:

    "You must choose a docroot that the system does not reserve"

    I checked Tweak Settings->Restrict document roots to public_html and it is ON.

    In my script I set up the call to the API using this URL:

    Code:
    $URI = "http://$g_accounthost:2082/json-api/cpanel?user=$g_accountuserid&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_func=addsubdomain&cpanel_jsonapi_apiversion=2&domain=$newsubdomain&rootdomain=$g_accountdomain
    This worked previously with no "dir" parameter being passed (i.e. default). Now it issues the above error message. In the cPanel API docs, it says for the "dir" parameter:
    ------------------------------------------------------------------------
    The subdomain's document root within your home directory.

    This parameter's default depends on the server's settings:
    cPanel & WHM version 58 and higher:
    - If the Restrict document roots to public_html value is On, the parameter defaults to the public_html/subdomain_namepath, where subdomain_name is the subdomain's name.
    - If the Restrict document roots to public_html value is Off, the parameter defaults to the subdomain_name path, where subdomain_name is the subdomain's name.

    ------------------------------------------------------------------------
    Since my setting is ON, the dir param should default to public_html/$newsubdomain in my case, which is what I want it to do, and that's where it created the new subdomains in the past on my old server without any problems.

    On the new server, however, if I explicitly add
    Code:
    &dir=public_html/$newsubdomain
    to the end of the the URL, it properly creates the subdomain as public_html/$newsubdomain as it used to.

    Since the Restrict document roots to public_html setting is ON, I would expect that if I didn't specify &dir, it would have defaulted to the same value I passed when I added the &dir=public_html/$newsubdomain parameter explicitly.

    Am I missing something or should my call work the old way (without &dir) if I want to create public_html/$newsubdomain?

    Thanks!
     
  2. cPanelLauren

    cPanelLauren Forums Analyst
    Staff Member

    Joined:
    Nov 14, 2017
    Messages:
    2,194
    Likes Received:
    156
    Trophy Points:
    143
    Location:
    Houston
    cPanel Access Level:
    DataCenter Provider
    Hi @swbrains

    I'm running this using the Command Line and not encountering any issues. Here's what I'm running:

    Code:
    # cpapi2 --user=example SubDomain addsubdomain domain=test.example.com rootdomain=example.com
    ---
    cpanelresult:
      apiversion: 2
      data:
        -
          reason: The subdomain “test.example.com” has been added.
          result: 1
      event:
        result: 1
      func: addsubdomain
      module: SubDomain
    I have also ensured that restrict document roots to public_html is on in tweak settings as well. I've tested with the JSON version as well:

    Code:
    https://server.example.com:2087/cpsess2448913101/json-api/cpanel?cpanel_jsonapi_user=example&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_func=addsubdomain&domain=subdomain.example.com&rootdomain=example.com
    With the result:
    Code:
    {"cpanelresult":{"data":[{"result":1,"reason":"The subdomain “subdomain.example.com” has been added."}],"apiversion":2,"module":"SubDomain","event":{"result":1},"func":"addsubdomain"}}
    Thanks!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. swbrains

    swbrains Well-Known Member

    Joined:
    Sep 13, 2006
    Messages:
    127
    Likes Received:
    15
    Trophy Points:
    168
    Hi,

    The only difference I can see between your params and mine is with the "domain" parameter value. The docs say:

    domain string Required The subdomain name to create. example: subdomain

    So in my code, I set &domain=test (for example), if I want to create test.example.com, whereas you have the full subdomain specified including the main domain its being created under.

    I haven't yet tried setting domain to "test.example.com" (without &dir) in my code to see if it makes a difference so I can't say if that's the difference.

    I've got rootdomain set as you do: example.com, which is the primary domain parked on the account. The account was created as a subdomain of my main business domain (for example, "custacct.domain.com"). Then I parked "example.com" on the customer account and tried to create the subdomain on "example.com". I doubt that all makes a difference, but I figured I'd mention it. :)

    Thanks!
     
  4. cPanelLauren

    cPanelLauren Forums Analyst
    Staff Member

    Joined:
    Nov 14, 2017
    Messages:
    2,194
    Likes Received:
    156
    Trophy Points:
    143
    Location:
    Houston
    cPanel Access Level:
    DataCenter Provider
    Hi @swbrains

    I tried modifying it a bit to not include the full subdomain:

    Code:
    https://server.example.com:2087/cpsess2448913101/json-api/cpanel?cpanel_jsonapi_user=example&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_func=addsubdomain&domain=test2&rootdomain=example.com
    The result:

    Code:
    {"cpanelresult":{"func":"addsubdomain","apiversion":2,"event":{"result":1},"module":"SubDomain","data":[{"reason":"The subdomain “test2.example.com” has been added.","result":1}]}}
    
    Let me try it with a parked domain as well, I'll update this thread shortly.


    Thanks!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. cPanelLauren

    cPanelLauren Forums Analyst
    Staff Member

    Joined:
    Nov 14, 2017
    Messages:
    2,194
    Likes Received:
    156
    Trophy Points:
    143
    Location:
    Houston
    cPanel Access Level:
    DataCenter Provider
    Same result for a parked domain as well:

    CLI:
    Code:
    # cpapi2 --user=example SubDomain addsubdomain domain=test3 rootdomain=parkeddomain.com
    ---
    cpanelresult:
      apiversion: 2
      data:
        -
          reason: The subdomain “test3.parkeddomain.com” has been added.
          result: 1
      event:
        result: 1
      func: addsubdomain
      module: SubDomain
    JSON
    Code:
    https://server.lauren.tech:2083/cpsess2083998335/json-api/cpanel?cpanel_jsonapi_user=example&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_func=addsubdomain&domain=test4&rootdomain=parkeddomain.com
    Result:
    Code:
    {"cpanelresult":{"module":"SubDomain","func":"addsubdomain","data":[{"result":1,"reason":"The subdomain “test4.parkeddomain.com” has been added."}],"apiversion":2,"event":{"result":1}}}
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
Loading...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice