Search code examples
c#jsongodaddy-api

JSON Schema for GoDaddy A_Record


Could someone please explain to me what the actual required JSON schema is to update the A Record of a Domain using the GoDaddy API ? I have tried variations of JSON schemas I found online, including their own up to date documentation, but can never, ever get it to work.

Yet, I can do any and all GETs without a single issue...

The error I get is a 422 http response back, and according to their own documentation, this means that the JSON sent, is of incorrect format / does not meet schema.

Here is the code used to submit the request, followed by the JSON formats I tried to use...

private async Task UpdateDNS()
{
        string domainName = "THISISMINE"; // my GoDaddy domain name
        string newIPAddress = GetIPAddress(); ; // My new IP address

        // Authenticate with the GoDaddy API using API key and secret
        string apiKey = "MY_PRODUCTION_API_KEY";
        string apiSecret = "MY_PRODUCTION_SECRET";

        string OTEKey = "MY_OTE_KEY";
        string OTESecret = "MY_OTE_SECRET";

        HttpClient httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("sso-key", $"{apiKey}:{apiSecret}");

        // Get the domain details from the GoDaddy API
        string getDomainURL = $"https://api.godaddy.com/v1/domains/{domainName}";
        HttpResponseMessage response = await httpClient.GetAsync(getDomainURL);

        if (!response.IsSuccessStatusCode)
        {
            this.textBox1.Text += "[" + DateTime.Now.ToLongTimeString() + "]: Failed to retrieve [GENERAL] domain details." + Environment.NewLine + Environment.NewLine;
            return;
        }

        string worker_json = await response.Content.ReadAsStringAsync();
        GoDaddyDomainInfo DomainInfo = JsonConvert.DeserializeObject<GoDaddyDomainInfo>(thejson);

       
        //Get the A record
        string aRecordName = "@";
        string aRecordURL = $"https://api.godaddy.com/v1/domains/{domainName}/records/A/{aRecordName}";
        
        //var updateRecordContent = new { data = newIPAddress };
       

        response = await httpClient.GetAsync(aRecordURL);

        if (!response.IsSuccessStatusCode)
        {
            this.textBox1.Text += "[" + DateTime.Now.ToLongTimeString() + "]: Failed to retrieve [A_RECORD] details for domain." + Environment.NewLine + Environment.NewLine;
            return;
        }


        worker_json = await response.Content.ReadAsStringAsync();
        List<GoDaddy_ARecord_Info> ARecordInfo = JsonConvert.DeserializeObject<List<GoDaddy_ARecord_Info>>(worker_json);

        ARecordInfo[0].data = newIPAddress;

        // Update the A record with the new IP address
       
        string updateRecordURL = $"https://api.godaddy.com/v1/domains/{domainName}/records/A/@";

        string jsonOUT = JsonConvert.SerializeObject(ARecordInfo);

        response = await httpClient.PutAsJsonAsync(updateRecordURL, jsonOUT);
        //THIS ALWAYS RETURNS 422 NO MATTER WHAT THE JSON LOOKS LIKE
        if (!response.IsSuccessStatusCode)
        {
            this.textBox1.Text += "[" + DateTime.Now.ToLongTimeString() + "]: Failed to UPDATE [A_RECORD] on GoDaddy." + Environment.NewLine + Environment.NewLine;
            return;
        }

        string p = $"IP address for {domainName} updated to {newIPAddress}.";

}

Here are the different JSON outputs that were tried without success:

[{"data":"xxx.xx.xx.xxx","ttl":3600}]

[{"data":"xxx.xx.xx.xxx","name":"@","ttl":3600,"type":"A"}]

[{"data":"xxx.xx.xx.xxx","name":"record","ttl":3600,"type":"A"}]

[{"data":"xxx.xx.xx.xxx","name":"%40","ttl":3600,"type":"A"}]

[{ "data": "xxx.xx.xx.xxx","name": "@","port": 65535,"priority": 0,"protocol": "string","service": "string","ttl": 3600,"type": "A","weight": 0}]

Please help... this should be ultra simplistic in nature and yet...


Solution

  • the bug is in using AsJSon. When you use ASJson you should not serialize data

    string jsonOUT = JsonConvert.SerializeObject(ARecordInfo); // remove this line
    
    response = await httpClient.PutAsJsonAsync(updateRecordURL, ARecordInfo);
    

    or another syntax that is the most common

    string jsonOUT = JsonConvert.SerializeObject(ARecordInfo);
    
    var content = new StringContent(jsonOUT, Encoding.UTF8, "application/json");   
             
    var message = await httpClient.PutAsync(updateRecordURL, content);