Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted
Updated 28-Sep-20 4:35am

1 solution

You are not calling a method. Your url is just https://localhost:44312/GeneratorStatus
 
Share this answer
 
Comments
stevenlam505 28-Sep-20 10:41am    
Isn't the, "await client.PostAsync(url, new StringContent(jsonMessage, Encoding.UTF8, "application/json"));" calling the PostAsync method on the client object?
ZurdoDev 28-Sep-20 10:44am    
It is, but the url is calling GeneratorStatus which is just the controller. It is not calling an action or method within the controller.

It should be .../GeneratorStatus/SomeMethod
stevenlam505 28-Sep-20 10:49am    
Oh I see. Do I need to add a new method into the GeneratorStatusController or just use ".../GeneratorStatus/Post"? If I were to add a new method, what method should I add?
ZurdoDev 28-Sep-20 10:53am    
Depends on what you want. I would suggest changing the names of your methods from Get and Post to something like GetStrings and PostStrings. Then in your url if you want to call GetStrings you would add that to the end of your url.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900