通过 httpclient 将字符串发布到 asp.net 核心 webapi 失败,请求错误
Posting a string to asp.net core webapi through httpclient fails with a bad request
我在将字符串发布到 Web 时收到来自 HttpClient 的错误请求 api。我知道如果我在 web api 中有一个对象作为参数,这将起作用,但我想要一个可以接受字符串和 return 字符串的方法。这个 api 在 POSTMAN 中有效。这是 api 和 HttpClient codeL
WebAPI 代码:
[HttpPost("TestMethod")]
public ActionResult<string> TestMethod([FromBody] string testString)
{
var s = "Hello: " + testString;
return Content(s.ToString());
}
Http 客户端代码:
string apiResponse="";
string API_URL = configuration["API_URL"].ToString() + "mauser/TestMethod";
var postParameters = new { testString = "Bob" };
var jsonString = JsonConvert.SerializeObject(postParameters);
var stringContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
using (var httpClient = new HttpClient())
{
using (var response = await httpClient.PostAsync(API_URL, stringContent))
{
if (response.IsSuccessStatusCode)
{
apiResponse = await response.Content.ReadAsStringAsync();
}
else
{
//Handle error
}
}
}
return apiResponse;
在 Visual Studio 中调试期间,客户端代码在 var response = await httpClient.PostAsync(API_URL, stringContent)
处失败
我收到的错误:
{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Transfer-Encoding: chunked
Server: Kestrel
X-Powered-By: ASP.NET
Date: Mon, 23 Nov 2020 01:36:44 GMT
Content-Type: application/problem+json; charset=utf-8
}}
因为在邮递员中,即使您将 JSON 设置为类型,您传递的也是纯文本(字符串)类型。您的控制器只适合接收字符串,因此它可以在邮递员中使用。
有效json:
{
"ActivationCode":"FFFF",
"Email": "joe@email.com"
}
您可以直接在 postAsync
中传递 jsonString
以使其从代码隐藏中运行。
由于您使用简单的字符串接收数据,因此无需将其设置为对象。
只需序列化字符串。
var postParameters = "Bob";
var jsonString = JsonConvert.SerializeObject(postParameters);
var stringContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
结果:
我在将字符串发布到 Web 时收到来自 HttpClient 的错误请求 api。我知道如果我在 web api 中有一个对象作为参数,这将起作用,但我想要一个可以接受字符串和 return 字符串的方法。这个 api 在 POSTMAN 中有效。这是 api 和 HttpClient codeL
WebAPI 代码:
[HttpPost("TestMethod")]
public ActionResult<string> TestMethod([FromBody] string testString)
{
var s = "Hello: " + testString;
return Content(s.ToString());
}
Http 客户端代码:
string apiResponse="";
string API_URL = configuration["API_URL"].ToString() + "mauser/TestMethod";
var postParameters = new { testString = "Bob" };
var jsonString = JsonConvert.SerializeObject(postParameters);
var stringContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
using (var httpClient = new HttpClient())
{
using (var response = await httpClient.PostAsync(API_URL, stringContent))
{
if (response.IsSuccessStatusCode)
{
apiResponse = await response.Content.ReadAsStringAsync();
}
else
{
//Handle error
}
}
}
return apiResponse;
在 Visual Studio 中调试期间,客户端代码在 var response = await httpClient.PostAsync(API_URL, stringContent)
我收到的错误:
{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Transfer-Encoding: chunked
Server: Kestrel
X-Powered-By: ASP.NET
Date: Mon, 23 Nov 2020 01:36:44 GMT
Content-Type: application/problem+json; charset=utf-8
}}
因为在邮递员中,即使您将 JSON 设置为类型,您传递的也是纯文本(字符串)类型。您的控制器只适合接收字符串,因此它可以在邮递员中使用。
有效json:
{
"ActivationCode":"FFFF",
"Email": "joe@email.com"
}
您可以直接在 postAsync
中传递 jsonString
以使其从代码隐藏中运行。
由于您使用简单的字符串接收数据,因此无需将其设置为对象。
只需序列化字符串。
var postParameters = "Bob";
var jsonString = JsonConvert.SerializeObject(postParameters);
var stringContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
结果: