c# - 来自 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
解决方案
- 我没有使用过这个端点,但是你缺少返回类型。
- 接下来我会尝试将 json 字符串直接粘贴到 StringContent 中。
- 请粘贴正确的字符串内容(重命名变量)
如果这些都对您没有帮助,请提供更多详细信息。
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
推荐阅读
- node.js - 在 Cypress 中模拟网络请求
- python - 尝试运行张量板时出现此错误?
- angular - TypeScript 数组:.map() 不是函数
- reactjs - react-select 不在移动设备上显示键盘并防止缩放
- mysql - 重复 MySQL 条目的输出列 ID
- python - 来自外部 Python 函数的 Keras 中的自定义损失
- python - Julia LightGraphs 中的 Networkx“祖先”等价性
- php - 在自定义路由器 PHP 中匹配 URL 上的参数
- excel - 即使不满足 IF-THEN 条件,也继续 FOR-NEXT 循环
- chart.js - Chart.js 字母间距很尴尬