使用 HttpClient 的简单 POST
Simple POST with HttpClient
我有两个 ASP.NET Core 2.1 应用程序,我正在尝试使用 HttpClient
.
从一个应用程序到另一个应用程序进行简单的 POST
调用
出于某种原因,当我使用 [FromBody]
获取我尝试接收的简单文本时,出现 BadRequest
错误。
这是我在发件人上的代码。首先,这是我的 ConfigureServices
方法中的内容。我正在使用 ASP.NET Core 2.1 中的新 HttpClientFactory
功能。我还创建了一个名为 myApiCallerClient
的客户端 class 来处理我的 API 调用:
services.AddHttpClient("myNamedClient", client =>
{
client.BaseAddress = new Uri("http://localhost:50625/api/");
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add("Accept", "application/json");
});
services.AddScoped(x => {
var httpClientFactory = x.GetRequiredService<System.Net.Http.IHttpClientFactory>();
var myApiCallerClient = httpClientFactory.CreateClient("myNamedClient");
return new Clients.ApiCaller.ApiCallerClient(myApiCallerClient);
});
这是 myApiCallerClient 中的代码:
public async Task SayHello()
{
var response = await _httpClient.PostAsync("test", new StringContent("Saying hello!", System.Text.Encoding.UTF8, "text/plain"));
}
这是我在接收端的代码,它是 TestController
中的 POST()
API 方法:
[HttpPost]
public async Task Post([FromBody]string input)
{
// Some logic
}
如果我使用 [FromBody]
并且在发件人上收到 BadRequest
错误,我的调用不会命中此方法。如果我使用 [FromHeader]
,我的请求会触发此 API 方法,但我得到一个 null
值。
我做错了什么?
ASP.NET Core 不支持开箱即用的 Content-Type
of text/plain
,因此服务器拒绝您的请求,因为它在使用[FromBody]
属性。
在评论中,您说:
Ultimately I won't be sending text. My main concern here is that I'm not hitting my API method. [...] Once I make sure everything is working, I'll be sending JSON data which will get deserialized on the receiving end.
为了测试问题是否是由 text/plain
Content-Type
引起的,您可以将 PostAsync
行更新为如下内容:
var response = await _httpClient.PostAsync(
"test",
new StringContent("\"Saying hello!\"", System.Text.Encoding.UTF8, "application/json"));
因为 application/json
是 默认情况下受支持的 Content-Type
,上面的代码使用它并用 "
包装您发送的值s 以使其成为有效的 JSON 值。
我有两个 ASP.NET Core 2.1 应用程序,我正在尝试使用 HttpClient
.
POST
调用
出于某种原因,当我使用 [FromBody]
获取我尝试接收的简单文本时,出现 BadRequest
错误。
这是我在发件人上的代码。首先,这是我的 ConfigureServices
方法中的内容。我正在使用 ASP.NET Core 2.1 中的新 HttpClientFactory
功能。我还创建了一个名为 myApiCallerClient
的客户端 class 来处理我的 API 调用:
services.AddHttpClient("myNamedClient", client =>
{
client.BaseAddress = new Uri("http://localhost:50625/api/");
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add("Accept", "application/json");
});
services.AddScoped(x => {
var httpClientFactory = x.GetRequiredService<System.Net.Http.IHttpClientFactory>();
var myApiCallerClient = httpClientFactory.CreateClient("myNamedClient");
return new Clients.ApiCaller.ApiCallerClient(myApiCallerClient);
});
这是 myApiCallerClient 中的代码:
public async Task SayHello()
{
var response = await _httpClient.PostAsync("test", new StringContent("Saying hello!", System.Text.Encoding.UTF8, "text/plain"));
}
这是我在接收端的代码,它是 TestController
中的 POST()
API 方法:
[HttpPost]
public async Task Post([FromBody]string input)
{
// Some logic
}
如果我使用 [FromBody]
并且在发件人上收到 BadRequest
错误,我的调用不会命中此方法。如果我使用 [FromHeader]
,我的请求会触发此 API 方法,但我得到一个 null
值。
我做错了什么?
ASP.NET Core 不支持开箱即用的 Content-Type
of text/plain
,因此服务器拒绝您的请求,因为它在使用[FromBody]
属性。
在评论中,您说:
Ultimately I won't be sending text. My main concern here is that I'm not hitting my API method. [...] Once I make sure everything is working, I'll be sending JSON data which will get deserialized on the receiving end.
为了测试问题是否是由 text/plain
Content-Type
引起的,您可以将 PostAsync
行更新为如下内容:
var response = await _httpClient.PostAsync(
"test",
new StringContent("\"Saying hello!\"", System.Text.Encoding.UTF8, "application/json"));
因为 application/json
是 默认情况下受支持的 Content-Type
,上面的代码使用它并用 "
包装您发送的值s 以使其成为有效的 JSON 值。