I've been trying to send POST messages where the message was from an appsettings.json file that I converted into a string.
What I have tried:
Program.cs
static async Task SendPost(string jsonMessage)
{
string url = "https://localhost:44312/GeneratorStatus";
HttpClient client = new HttpClient();
var response = await client.PostAsync(url, new
StringContent(jsonMessage,
Encoding.UTF8, "application/json"));
Console.WriteLine(response);
}
jsonMessage in this case is just a string that reads:
TestLaptop has encountered an error at 09.28.2020 09:15 AM/email1@gmail.com/email2@gmail.com/email3@gmail.com
This is my controller:
GeneratorStatusController.cs
[ApiController]
[Route("[controller]")]
public class GeneratorStatusController : ControllerBase
{
Emails emailList = new Emails();
static List<string> strings = new List<string>()
{
"value0", "value1", "value2"
};
[HttpGet]
public List<string> Get()
{
return strings;
}
[HttpPost("{input}")]
public List<string> Post(string input)
{
strings.Add(input);
return strings;
}
}
When running my program.cs while having IIS express hosting my server, I keep receiving a 405 error when running program.cs. This is the full "response" variable:
StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Transfer-Encoding: chunked
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
Date: Mon, 28 Sep 2020 13:18:49 GMT
Allow: GET
}
[09:18:49 INF] Ending service