Click here to Skip to main content
15,887,318 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am able to send a post request to a server using:
C#
static async Task SendPost(string jsonMessage)
{
    string url = "https://localhost:44312/GeneratorStatus/PostString";
    HttpClient client = new HttpClient();
    
    var response = await client.PostAsync(url, 
                                          new StringContent("TestMessage",
                                                            Encoding.UTF8,
                                                            "application/json")
                                         );

    Console.WriteLine(response);
}

This is my controller:
C#
[ApiController]
[Route("[controller]")]
public class GeneratorStatusController : ControllerBase
{
    Emails emailList = new Emails();

    [HttpPost("{input}")]
    public void PostString(string input)
    {
        emailList.SendEmail("fromEmail@gmail.com", "1234567890@txt.att.net",
        "Generator Off", input);
    }
}


However, when I run my post request using HttpClient and send a post request to my API Controller, it sends out the request successfully but the message always results in, "PostString" instead of my input variable.

What I have tried:

C#
[ApiController]
[Route("[controller]")]
public class GeneratorStatusController : ControllerBase
{
    Emails emailList = new Emails();

    [HttpPost("{input}")]
    public void PostString(string input)
    {
        emailList.SendEmail("fromEmail@gmail.com", "1234567890@txt.att.net",
        "Generator Off", input);
    }
}
Posted
Updated 28-Sep-20 7:38am
v3
Comments
Oshtri Deka 28-Sep-20 12:12pm    
Where is return statement of PostString method?
stevenlam505 28-Sep-20 13:26pm    
I had it in the original code and forgot to make it void when I posted the question.

1 solution

Quote:
C#
[HttpPost("{input}")]
You've bound the input parameter to the route. Whatever appears in the URL after /GeneratorStatus/ will be the value of your input parameter.

Change your API method to:
C#
[HttpPost]
public void PostString([FromBody] string input)
{
    ...
}
Change the calling code to specify the correct URL, and pass the content correctly:
C#
string url = "https://localhost:44312/GeneratorStatus/";
HttpClient client = new HttpClient();
string input = JsonConvert.SerializeObject(jsonMessage);
var response = await client.PostAsync(url, new StringContent(input, Encoding.UTF8, "application/json"));
NB: If you upload the data as JSON, it needs to be valid JSON. If you want to post a string, it needs to be enclosed in double quotes and properly escaped. Just posting the raw string value won't work.

Model Binding in ASP.NET Core | Microsoft Docs[^]

NB: You should also be using IHttpClientFactory to create the HttpClient instance, otherwise you risk running into problems under load:
Use IHttpClientFactory to implement resilient HTTP requests | Microsoft Docs[^]
 
Share this answer
 

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