GoDaddy API更新A记录所需JSON Schema技术求助
GoDaddy API更新域名A记录返回422错误排查与解决
本人使用GoDaddy API更新域名A记录时,所有GET请求均可正常执行,但PUT请求始终返回422 HTTP响应(据官方文档,该错误表示提交的JSON格式不符合Schema要求)。尝试了多种从网络及官方文档获取的JSON格式均未成功,以下是使用的C#代码及尝试过的JSON示例,恳请告知正确的JSON Schema:
使用的C#代码
private async Task UpdateDNS() { string domainName = "THISISMINE"; // 我的GoDaddy域名 string newIPAddress = GetIPAddress(); ; // 新的IP地址 // 使用API密钥和密钥验证GoDaddy API 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}"); // 从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() + "]: 获取[通用]域名详情失败。" + Environment.NewLine + Environment.NewLine; return; } string worker_json = await response.Content.ReadAsStringAsync(); GoDaddyDomainInfo DomainInfo = JsonConvert.DeserializeObject<GoDaddyDomainInfo>(thejson); // 获取A记录 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() + "]: 获取域名[A记录]详情失败。" + 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; // 更新A记录为新IP地址 string updateRecordURL = $"https://api.godaddy.com/v1/domains/{domainName}/records/A/@"; string jsonOUT = JsonConvert.SerializeObject(ARecordInfo); response = await httpClient.PutAsJsonAsync(updateRecordURL, jsonOUT); // 无论JSON格式如何,这里始终返回422 if (!response.IsSuccessStatusCode) { this.textBox1.Text += "[" + DateTime.Now.ToLongTimeString() + "]: GoDaddy上[A记录]更新失败。" + Environment.NewLine + Environment.NewLine; return; } string p = $"域名{domainName}的IP地址已更新为{newIPAddress}。"; }
尝试过的JSON格式
[{"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}]
问题根源与正确实现
核心错误
你遇到的422错误本质是代码中使用PutAsJsonAsync时传入了已序列化的JSON字符串,导致API接收到双重序列化的内容(比如原JSON被转义为"[{\"data\":\"xxx.xx.xx.xxx\"...}]"),完全不符合Schema要求。
正确的A记录JSON Schema
针对GoDaddy API的单类型A记录PUT请求,正确的请求体是包含单条记录的数组,必填字段仅data,ttl为可选(默认3600),无需传入name、type(已在URL中指定A/@):
[{"data":"xxx.xx.xx.xxx","ttl":3600}]
修正后的代码关键部分
无需手动序列化对象,直接将List<GoDaddy_ARecord_Info>对象传给PutAsJsonAsync,它会自动完成正确的JSON序列化:
// 移除手动序列化代码 // string jsonOUT = JsonConvert.SerializeObject(ARecordInfo); // 直接传入对象,而非序列化后的字符串 response = await httpClient.PutAsJsonAsync(updateRecordURL, ARecordInfo);
同时确保GoDaddy_ARecord_Info类定义仅包含必要字段:
public class GoDaddy_ARecord_Info { public string data { get; set; } public int ttl { get; set; } // A记录不需要port/priority等字段,可删除多余属性 }
额外验证点
- 确认使用生产环境API密钥,而非OTE(测试环境)密钥,URL对应
api.godaddy.com - 检查
newIPAddress是合法的IPv4地址,格式无误 - 确保API密钥拥有域名DNS修改权限
内容的提问来源于stack exchange,提问作者MaxOvrdrv
相关产品推荐
相关产品推荐

