You need to add the
[FromBody]
attribute to your
input
parameter:
[HttpPost]
public List<string> PostString([FromBody] string input)
Model Binding in ASP.NET Core | Microsoft Docs[
^]
You should also remove the extra serialization and deserialization from your calling method. There's no point serializing to JSON simply to deserialize again on the next line.
However, since the method input is a string, you
will need to double-encode your request body. Otherwise, the media formatter will decode the supplied JSON, and then refuse to bind the string array to a
string
parameter.
static async Task SendPost(string machineName, List<string> emailAddresses, List<string> phoneNumbers)
{
string url = "https://localhost:44312/GeneratorStatus/";
HttpClient client = new HttpClient();
var jsonMessage = JsonConvert.SerializeObject(new[]
{
machineName,
emailAddresses,
phoneNumbers
});
Console.WriteLine(jsonMessage);
jsonMessage = JsonConvert.SerializeObject(jsonMessage);
Console.WriteLine(jsonMessage);
var response = await client.PostAsync(url, new StringContent(jsonMessage, Encoding.UTF8, "application/json"));
Console.WriteLine(response);
}
As mentioned in the solution to
your previous question[
^], you should really be using a strongly-typed model instead of passing a JSON string around.