调用 POST 方法时出现内部服务器错误
Internal Server Error when calling POST method
我正在尝试创建一个控制台应用程序,它能够通过调用某个 API 将内容推送到数据库中。我无法控制数据库或 API。 API 工作正常,因为我已经创建并测试了 GET 方法,但我想我的 POST.
有问题
首先,我创建了 Product 对象,其中包含我要发送到 API.
的值
Product product = new Product
{
productId = "27795",
amount = 1,
customerid = 0,
returnbasketid = true
};
这是应该调用 API 的 POST 方法的方法。
static async Task CreateProductAsync(HttpClient cons, Product product)
{
using (cons)
{
HttpResponseMessage res = cons.PostAsJsonAsync("", product).Result;
res.EnsureSuccessStatusCode();
Console.WriteLine(res.Content.ReadAsStringAsync().Result);
}
}
根据 运行 代码,第 HttpResponseMessage res = cons.PostAsJsonAsync("", product).Result;
行有以下错误:
System.AggregateException: 'One or more errors occurred. (Response status code does not indicate success: 500 (Internal Server Error).)'
HttpRequestException: Response status code does not indicate success: 500 (Internal Server Error).
我想问题是我如何将值发送到 API 以及 API 期望什么。我有这个数据预期示例:
{
"":[{
"productId":"27795",
"amount":2,
"customerid":0
},{
"productId":"27796",
"amount":1,
"customerid":0
}],
"returnbasketid":true
}
我已经使用 Postman 对此进行了测试,它工作正常,所以问题一定出在我的代码上。
https://pastebin.com/pZDehaaD 这是我的 Program.cs 的完整示例。不过 API url 已被删除。
编辑:所以这是我的控制台应用程序发送的 JSON:
而通过 Postman 工作的 JSON 是这样的:
所以问题是我应该如何编写代码才能正确配置我发送的 JSON?
您正在将产品传递给 API(这是上面的第一个 Fiddler 捕获)。
但是如果我们查看所需的输入(上面的第二个 Fiddlel 捕获)以及 "data expected example"(灰色背景),我们会看到一个对象,其中包含一个(未命名的)Products 数组和名为"returnbasketid".
如果重新创建这个对象,我们需要做什么。我们可以将此 class 称为 "Wrapper" 或 "MyRequestObject" 或任何其他名称。名字并不重要。我会尝试这种形式的代码。
public class Wrapper
{
[JsonProperty("")] // rename json property to empty string - don't know if this will work
public List<Product> Products { get; set; } = new List<Product>();
public bool returnbasketid { get; set; }
}
然后,您将此 Wrapper 的实例而不是 Product 传递给 with cons.PostAsJsonAsync() 调用。
问题在于我们的 Wrapper 的 Product 数组有一个名称,而服务器想要一个没有名称的数组。我正在尝试重命名上面的数组。如果那很好用。否则,我认为您将不得不自己创建 json(使用 Newtonsoft 很容易),然后使用 HttpClient 的 PostAsync(Uri, HttpContent).
创建 post
我正在尝试创建一个控制台应用程序,它能够通过调用某个 API 将内容推送到数据库中。我无法控制数据库或 API。 API 工作正常,因为我已经创建并测试了 GET 方法,但我想我的 POST.
有问题首先,我创建了 Product 对象,其中包含我要发送到 API.
的值Product product = new Product
{
productId = "27795",
amount = 1,
customerid = 0,
returnbasketid = true
};
这是应该调用 API 的 POST 方法的方法。
static async Task CreateProductAsync(HttpClient cons, Product product)
{
using (cons)
{
HttpResponseMessage res = cons.PostAsJsonAsync("", product).Result;
res.EnsureSuccessStatusCode();
Console.WriteLine(res.Content.ReadAsStringAsync().Result);
}
}
根据 运行 代码,第 HttpResponseMessage res = cons.PostAsJsonAsync("", product).Result;
行有以下错误:
System.AggregateException: 'One or more errors occurred. (Response status code does not indicate success: 500 (Internal Server Error).)'
HttpRequestException: Response status code does not indicate success: 500 (Internal Server Error).
我想问题是我如何将值发送到 API 以及 API 期望什么。我有这个数据预期示例:
{
"":[{
"productId":"27795",
"amount":2,
"customerid":0
},{
"productId":"27796",
"amount":1,
"customerid":0
}],
"returnbasketid":true
}
我已经使用 Postman 对此进行了测试,它工作正常,所以问题一定出在我的代码上。
https://pastebin.com/pZDehaaD 这是我的 Program.cs 的完整示例。不过 API url 已被删除。
编辑:所以这是我的控制台应用程序发送的 JSON:
而通过 Postman 工作的 JSON 是这样的:
所以问题是我应该如何编写代码才能正确配置我发送的 JSON?
您正在将产品传递给 API(这是上面的第一个 Fiddler 捕获)。
但是如果我们查看所需的输入(上面的第二个 Fiddlel 捕获)以及 "data expected example"(灰色背景),我们会看到一个对象,其中包含一个(未命名的)Products 数组和名为"returnbasketid".
如果重新创建这个对象,我们需要做什么。我们可以将此 class 称为 "Wrapper" 或 "MyRequestObject" 或任何其他名称。名字并不重要。我会尝试这种形式的代码。
public class Wrapper
{
[JsonProperty("")] // rename json property to empty string - don't know if this will work
public List<Product> Products { get; set; } = new List<Product>();
public bool returnbasketid { get; set; }
}
然后,您将此 Wrapper 的实例而不是 Product 传递给 with cons.PostAsJsonAsync() 调用。
问题在于我们的 Wrapper 的 Product 数组有一个名称,而服务器想要一个没有名称的数组。我正在尝试重命名上面的数组。如果那很好用。否则,我认为您将不得不自己创建 json(使用 Newtonsoft 很容易),然后使用 HttpClient 的 PostAsync(Uri, HttpContent).
创建 post