简体   繁体   English

为什么我的HTTP POST正文内容被接收/处理为NULL?

[英]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.

相关问题 为什么添加Microsoft.AspNetCore.OData.Versioning后,我的HTTP Post不再传递正文内容 - Why is my HTTP Post no longer passing the body content after adding Microsoft.AspNetCore.OData.Versioning Http Post发送带有空正文的请求 - Http Post is sending request with null body 从我的Web服务中读取Http POST正文内容? (.net 3.5) - Reading the Http POST body content from my web service? (.net 3.5) 使用AngularJS和Web api 2,通过http post收到的值为null - Values received via http post are null with AngularJS and Web api 2 当HTTP-POST具有主体时,url参数为null - When HTTP-POST has body, url parameter is null HTTP Post请求正文值null c# - HTTP Post request body values null c# C# HTTP 服务器 POST 请求将正文解析为 null - C# HTTP Server POST request is parsing the body as null 为什么我的Ajax帖子在进入后端时有内容,但是我的模型和控制器却收到null - Why does my Ajax post have content when going to backend but my model and controller receive null 从我的SPA FETCH POST接收到的控制器viewmodel值为null - Controller viewmodel values are received as null from my SPA FETCH POST 为什么我的内容主体通过编码? - Why is my content body coming through encoded?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM