简体   繁体   English

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

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

I have create gandi api code for create domain and for that i have write below code, but it show me 400 bad request error我已经为创建域创建了 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);     
}

but when i try to pass same data using postman then it's working fine below code is my postman data但是当我尝试使用 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. I havent worked with this endpoint, but you are missing the return type.我没有使用过这个端点,但是你缺少返回类型。
  2. the next thing i would try is to paste json string directly in the StringContent.接下来我会尝试将 json 字符串直接粘贴到 StringContent 中。
  3. please paste the correct string content(rename the variable)请粘贴正确的字符串内容(重命名变量)

if none of this help you, please give more details.如果这些都对您没有帮助,请提供更多详细信息。

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  1. I havent worked with this endpoint, but you are missing the return type.我没有使用过这个端点,但是你缺少返回类型。
  2. the next thing i would try is to paste json string directly in the StringContent.接下来我会尝试将 json 字符串直接粘贴到 StringContent 中。
  3. please paste the correct string content(rename the variable)请粘贴正确的字符串内容(重命名变量)

if none of this help you, please give more details.如果这些都对您没有帮助,请提供更多详细信息。

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

For the https://api.gandi.net/v5/domain/domains endpoint, use HTTP GET ( HttpClient.GetAsync ) to retrieve a list of your domains.对于https://api.gandi.net/v5/domain/domains端点,使用 HTTP GET ( HttpClient.GetAsync ) 检索您的域列表。 Use HTTP POST ( HttpClient.PostAsync ) to create a new domain.使用 HTTP POST ( HttpClient.PostAsync ) 创建一个新域。

If you're trying to POST JSON, I would use the PostAsJsonAsync method, example here :如果您尝试发布POST ,我将使用PostAsJsonAsync方法, 例如

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

Also note your auth header needs to start with "apikey" though it looks like you have that working.另请注意,您的身份验证 header 需要以“apikey”开头,尽管看起来您可以正常工作。 Curl example: Curl 示例:

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

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

暂无
暂无

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

相关问题 API响应在Postman中返回0,但从C#调用时返回-0.0 - API response returns 0 in Postman, but -0.0 when calling from C# 从Postman工作正常的Windows窗体进行API调用超时 - API call going in timeout from windows forms working fine from Postman 来自C#的Google Drive API错误请求,但来自Postman的罚款 - Google Drive API bad request from C# but fine from Postman HTTPClient 返回 400 Bad Request 但 Postman 在 C# 中返回 201 - HTTPClient returns 400 Bad Request but Postman returns 201 in C# C#REST API调用 - 在Postman中工作,而不是在Code中 - C# REST API Call - Working in Postman, NOT in Code 我有在天蓝色上烫过的网络API。 从邮递员那里得到请求,然后简单地得到请求。 但它不能从C#http get调用 - I have web api which is hoted on azure. get request working from postman and simple get request. But it not working from c# http get call Post Rest Web API在Postman上工作,但未从C#项目中的代码返回任何响应 - Post rest web api working from Postman but not returning any response from code in C# project Binance API调用可以在控制台应用程序上正常运行,但不能在WinForm c#上运行 - Binance API call working fine on Console Application but not on WinForm c# 从C#调用获取404,从Postman获取200 - Getting 404 from C# call but 200 from Postman 从 C# 应用程序发送时 Flask 400 错误请求,但来自邮递员的 200? - Flask 400 Bad Request when sent from C# App, but 200 from Postman?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM