[英]Why is my HTTP POST Body content received/processed as NULL?
I want to be able to receive the Body content of a POST request inside the POST function definition in the REST API. 我希望能够在REST API的POST函数定义中接收POST请求的主体内容。
I have a client code that converts a C# object into JSON and then wraps it in a HTTP StringContent. 我有一个客户端代码,该代码将C#对象转换为JSON,然后将其包装在HTTP StringContent中。 This payload is then sent via a HTTP Post request to the URL.
然后,此有效负载通过HTTP Post请求发送到URL。 However the Post method in API always returns NULL when I try returning the received string.
但是,当我尝试返回接收到的字符串时,API中的Post方法始终返回NULL。
Client: 客户:
public async void Register_Clicked(object sender, EventArgs e) // When user enters the Register button
{
Sjson = JsonConvert.SerializeObject(signup);
var httpContent = new StringContent(Sjson);
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("https://apiname.azurewebsites.net");
var response = await client.PostAsync("api/values", httpContent);
var responseContent = await response.Content.ReadAsStringAsync();
StatusLabel.Text = responseContent; //To display response in client
}
}
API POST Definition: API POST定义:
[SwaggerOperation("Create")]
[SwaggerResponse(HttpStatusCode.Created)]
public string Post([FromBody]string signup)
{
return signup;
}
I want the clients input back as a response to be displayed in the client (StatusLabel.Text). 我希望客户端输入作为响应显示在客户端(StatusLabel.Text)中。 However all I receive is NULL .
但是,我收到的只是NULL 。 Kindly guide me in the right direction.
请指引我正确的方向。
I have prepared a simple example on how you can retrieve your Result
from your Task
: 我准备了一个简单的示例,说明如何从
Task
检索Result
:
async Task<string> GetResponseString(SigupModel signup)
{
Sjson = JsonConvert.SerializeObject(signup);
var httpContent = new StringContent(Sjson);
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("https://apiname.azurewebsites.net");
var response = await client.PostAsync("api/values", httpContent);
var responseContent = await response.Content.ReadAsStringAsync();
}
return responseContent;
}
And you can call this as: 您可以将其称为:
Task<string> result = GetResponseString(signup);
var finalResult = result.Result;
StatusLabel.Text = finalResult; //To display response in client
If you are not using async
keyword, then you can call get your Result
as: 如果您没有使用
async
关键字,则可以调用get Result
作为:
var responseContent = response.Content.ReadAsStringAsync().Result;
EDIT 编辑
Basically you would have to POST your signup
model to your API Controller. 基本上,您必须将
signup
模型发布到API控制器。 Use SigupModel signup
as a parameter if you would like. 如果需要,可以使用
SigupModel signup
作为参数。 Then whatever processing you are doing in the API, the final result in a string. 然后,无论您在API中执行什么处理,最终结果都将是一个字符串。 Now this string can either be a null, empty or have a value.
现在,此字符串可以为null,空或具有值。 When you get this value in your client, you would have do the following:
当您在客户端中获得此值时,您将需要执行以下操作:
var responseContent = await response.Content.ReadAsStringAsync();
var message = JsonConvert.DeserializeObject<string>(responseContent);
Here you will get your message a string and then you can set it as: StatusLabel.Text = message ;
在这里,您将获得一个字符串消息,然后可以将其设置为:
StatusLabel.Text = message ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.