I want to add domain Alias to Plesk via API
public static HttpClient _client;
public PleskAPI(string baseUrl, string username, string password)
{
_client = new HttpClient { BaseAddress = new Uri(baseUrl) };
var byteArray = Encoding.ASCII.GetBytes($"{username}:{password}");
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
}
public static async Task<bool> AddDomain(string domainName, string ftpLogin, string ftpPassword, string ipv4, string planName)
{
var requestBody = new { name = domainName, hosting_type = "virtual", hosting_settings = new { ftp_login = ftpLogin, ftp_password = ftpPassword }, ipv4 = new[] { ipv4 }, plan = new { name = planName } };
var jsonContent = new StringContent(JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json");
var response = await _client.PostAsync("/api/v2/domains", jsonContent);
var responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseContent);
return response.IsSuccessStatusCode;
}
public static async Task<bool> AddDomainAlias(string aliasName, string domainName)
{
var xmlRequest = $@"
<packet version=""1.6.7.0"">
<site-alias>
<create>
<pref>
<web>1</web>
<mail>0</mail>
<tomcat>0</tomcat>
<seo-redirect>0</seo-redirect>
</pref>
<site-id>16</site-id>
<name>{aliasName}</name>
</create>
</site-alias>
</packet>";
var xmlContent = new StringContent(xmlRequest, Encoding.UTF8, "application/xml");
var response = await _client.PostAsync("/enterprise/control/agent.php", xmlContent);
var responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseContent);
return response.IsSuccessStatusCode;
}
bool success = await PleskAPI.AddDomainAlias(domainAlias, domainParent);
In the response of AddDomainAlias I get the result 200 OK. However in responseContent the result returned is:
<errcode>1029</errcode>
<errtext>Authentication method is not specified</errtext>
I checked it also added Authorization. Meanwhile, when I use AddDomain
via API and the result is successful, it also created the domain on my server.
I can’t find any other documentation about create AddDomainAlias
. Looking forward to everyone’s help. Thanks.