首页 > 解决方案 > 来自 C# 的 gandi api 调用返回 400。Postman 的工作正常

问题描述

我已经为创建域创建了 gandi api 代码,为此我写了下面的代码,但它显示了 400 bad request 错误

public async System.Threading.Tasks.Task<JsonResult> InsertDomain(DomainDetails domainDetails)
{
  HttpResponseMessage response = new HttpResponseMessage();
  try
  {
    var url = "https://api.gandi.net/v5/domain/domains";

    using ( var client = new HttpClient() )
    {
      var json = new JavaScriptSerializer().Serialize(domainDetails);
      HttpContent HttpContent = new StringContent(json, Encoding.UTF8, "application/json");
      var MyHttpClient = new HttpClient();
      MyHttpClient.DefaultRequestHeaders.Add("authorization", GANDI_API_Key);
      response = await MyHttpClient.PostAsync(url, HttpContent);
    }
  }
  catch ( Exception ex )
  {
    throw;
  }
  return Json(new { result = response }, JsonRequestBehavior.AllowGet);     
}

但是当我尝试使用邮递员传递相同的数据时,它工作正常下面的代码是我的邮递员数据

Body
{
  "fqdn":"dedasdom1906.com",
  "owner":
    {
      "city":"Paris",
      "given":"Alice",
      "family":"Doe",
      "zip":"75001",
      "country":"FR",
      "streetaddr":"5 rue neuve",
      "phone":"+33.123456789",
      "state":"FR-J",
      "type":"0",
      "email":"alice@example.org"
      }
}

Header
authorization : Apikey
Content-Type : application/json

标签: c#json

解决方案


  1. 我没有使用过这个端点,但是你缺少返回类型。
  2. 接下来我会尝试将 json 字符串直接粘贴到 StringContent 中。
  3. 请粘贴正确的字符串内容(重命名变量)

如果这些都对您没有帮助,请提供更多详细信息。

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

推荐阅读