繁体   English   中英

gandi api 从 C# 调用返回 400。它的工作正常从 Postman

[英]gandi api call from C# returns 400. Its Working Fine from Postman

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

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);     
}

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

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

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

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

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

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

对于https://api.gandi.net/v5/domain/domains端点,使用 HTTP GET ( HttpClient.GetAsync ) 检索您的域列表。 使用 HTTP POST ( HttpClient.PostAsync ) 创建一个新域。

如果您尝试发布POST ,我将使用PostAsJsonAsync方法, 例如

static async Task<Uri> CreateProductAsync(Product product)
{
    HttpResponseMessage response = await client.PostAsJsonAsync(
        "api/products", product);
         ...

另请注意,您的身份验证 header 需要以“apikey”开头,尽管看起来您可以正常工作。 Curl 示例:

curl -X GET \
  https://api.gandi.net/v5/domain/domains \
  -H 'authorization: Apikey your-api-key'

https://api.gandi.net/docs/domains/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM