Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The code I'm working on is an API that sends out a text message and email when a generator shuts off. When the generator shuts off, a post request is sent with a machine name, post message, a list of emails, and a list of phone numbers. However, I'm not sure how to send the machine name, post messages, list of emails, phone numbers into a POST request AND have the POST request parse the list and send out the emails and texts. I have included my current code:

What I have tried:

Console app that sends the post message:
static async Task SendPost(string machineName, string postMessage, List<string> emailAddresses, List<string> phoneNumbers)
{
    string url = "https://localhost:44312/GeneratorStatus/";
    HttpClient client = new HttpClient();

    string name = JsonConvert.SerializeObject(machineName);
    string post= JsonConvert.SerializeObject(postMessage);
    string emails = JsonConvert.SerializeObject(emailAddresses);
    string phones = JsonConvert.SerializeObject(phoneNumbers);

    var jsonMessage= JsonConvert.SerializeObject(new[] 
              {JsonConvert.DeserializeObject(name),
                                                  
               JsonConvert.DeserializeObject(post),
                                                  
               JsonConvert.DeserializeObject(emails),
                                                  
               JsonConvert.DeserializeObject(phones) });

    var response = await client.PostAsync(url, new StringContent(jsonMessage, 
    Encoding.UTF8, "application/json"));
    Console.WriteLine(response);
}


My API Controller:
[HttpPost]
public void PostString([FromBody] string jsonString)
{
    emailList.SendEmail("fromEmail@gmail.com", <emails and phones> , <name>, <post>);
}


My question is just, how do I format my API controller so it is able to accept and parse the information of "jsonMessage" and then format it in the way I wanted above?
Posted
Updated 28-Sep-20 20:53pm
v2
Comments
ZurdoDev 28-Sep-20 16:07pm    
If all of the data is coming into jsonString, then you just need to google c# deserialize json string and you'll have lots of examples of how to parse it.
Afzaal Ahmad Zeeshan 28-Sep-20 23:23pm    
JSON is just there to help you with the data interchange between the server and the client application. If your client-side part contains the styling, then your server should have that as well.

I would like to recommend using email templates; either custom or mail vendor provided ones. They would help you in many ways.

Oh, and always try to prevent HTML content that is not validated for XSS and other forgery scripts, it would lead to a big trouble for your service in future.

1 solution

The problem that you have here is that you're trying to hide a complex object inside a simple representation. Rather than passing a string around, you should make a data contract that encapsulates the email message. Something like this perhaps:
C#
[DataContract]
public class EmailMessage
{
  [DataMember(Name = "MachineName", EmitDefaultValue = false)]
  public string MachineName { get; set; }
  [DataMember(Name = "MachineName", EmitDefaultValue = false)]
  public string PostMessage { get; set; }
  [DataMember(Name = "MachineName", EmitDefaultValue = false)]
  public List<string> EmailAddresses { get; set; }
  [DataMember(Name = "MachineName", EmitDefaultValue = false)]
  public List<string> PhoneNumbers { get; set; }
}
With that in place, you use the EmailMessage to build up the email in your console application and change your ResponseBody from a string to EmailMessage.
 
Share this answer
 
Comments
stevenlam505 29-Sep-20 8:15am    
What does your solution "do"? Sorry for the likely stupid question, I'm still very new to C#. My main goal currently is to move the JSON string into my API with this
[HttpPost]
public List<string> PostString([FromBody] string input)
{
strings.Add(input);
return strings;
}
but when I do [FromBody] string input I encounter error 400, but when I remove the [FromBody] portion, it posts but the input value is null.
Pete O'Hanlon 29-Sep-20 9:11am    
Your API would look something like this:
[HttpPost]
public IActionResult Post([FromBody]EmailMessage email)
{
// the email message here should match the email you posted in your console
return Ok();
}
stevenlam505 29-Sep-20 9:49am    
How do I format my PostRequest in the console app to make it line up with the intended parameters? Specifically the, "await client.PostAsync" line.

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