简体   繁体   中英

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.

I have a client code that converts a C# object into JSON and then wraps it in a HTTP StringContent. This payload is then sent via a HTTP Post request to the URL. However the Post method in API always returns NULL when I try returning the received string.

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:

[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). However all I receive is NULL . Kindly guide me in the right direction.

I have prepared a simple example on how you can retrieve your Result from your Task :

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:

var responseContent =  response.Content.ReadAsStringAsync().Result;

EDIT

Basically you would have to POST your signup model to your API Controller. Use SigupModel signup as a parameter if you would like. Then whatever processing you are doing in the API, the final result in a string. Now this string can either be a null, empty or have a value. 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 ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM