locked
Need Basic Web API example for my model project RRS feed

  • Question

  • User723941679 posted

    Hi,

    I want to use Web API for embedded machine communication. I have an embedded machine that sends 3 parameters value to Web API. In return I get 4 parameters value.

    I want to send CardNo, CardPass, SerialNo from embedded machine to Web API and in return I want to receive CustName, CustSurname, Location and Amount.

    Can anypne shows me how to do that?

    Kind Regards

    Friday, March 4, 2016 7:00 AM

Answers

  • User36583972 posted

    Hi NTMS2016,

    According to your description, I have made a sample on my side. You can try the following code.

    Define two classes in the API, to receive and return data:

    public class SendModel
        {
            public string CustName { set; get; }
            public string CustSurname { set; get; }
            public string Location { set; get; }
    
            public string Amount { set; get; }
    
        }
    
        public class ReceiveModel
        {
            public string CardNo { set; get; }
            public string CardPass { set; get; }
            public string SerialNo { set; get; }
        }

    In ValuesController, we are increasing the Post method:

    public class ValuesController : ApiController
        {
    
            // GET api/values
            [AllowAnonymous]
            public IEnumerable<string> Get()
            {
                return new string[] { "value1", "value2" };
            }
    
            // GET api/values/5
            public string Get(int id)
            {
                return "value";
            }
    
            // POST api/values
            [AllowAnonymous]
            public SendModel Post([FromBody]ReceiveModel receiveModel)
            {
                string serialNo = receiveModel.SerialNo;
                    SendModel sd = new SendModel() { Amount = "1", CustName = "name", CustSurname = "hello ", Location = "Yoko" };
                   return sd;
            }
    
            // PUT api/values/5
            public void Put(int id, [FromBody]string value)
            {
            }
    
            // DELETE api/values/5
            public void Delete(int id)
            {
            }
        }
    

    Finally, we call on the page with Ajax.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="Scripts/jquery-1.10.2.js"></script>
        <script src="http://code.angularjs.org/1.1.5/angular.min.js"></script>
        <script>
            $(document).ready(function () {
                $('#btnmethod').click(function () {
                    var userWeightings = [];
                    $('#sliders input[type=range]').each(function (i, obj) {
                        var sliderName = $(obj).attr('name');
                        var sliderValue = $(obj).val();
                        var userWeighting = { RuleName: sliderName, Weighting: sliderValue };
                        userWeightings.push(userWeighting);
                    })
                    var str = JSON.stringify(userWeightings);
                    alert(str);
                    $.ajax({
                        url: "http://localhost:3057/api/Values",
                        type: "Post",
                        contentType: "application/json; charset=urf-8",
                        dataType: "json",
                        //data: "{ RuleName:'slider1',Weighting:200}",
                        //data: str,
                        data: "{'CardNo':'001','CardPass':'XX002','SerialNo':'B6678'}",
                        success: function (response) {
                            alert(JSON.stringify(response))
                        }, 
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert("error-Put:" + textStatus + "  " + errorThrown);
                        }
                    });
                });
            });
        </script>
    
    
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:TextBox ID="TextBox1" runat="server" Height="31px" Width="417px"></asp:TextBox>
            <br />
            <asp:Button ID="btnmethod" runat="server" Text="Method" />
            <br />
            <br />
            <div>
            </div>
            <br />
            <div id="sliders">
                <div>
                    <input type="range" name="slider1" min="0" max="10">
                </div>
                <div>
                    <input type="range" name="slider2" min="0" max="10">
                </div>
                <div>
                    <input type="range" name="slider3" min="0" max="10">
                </div>
                <div>
                    <input type="range" name="slider4" min="0" max="10">
                </div>
                <div>
                    <input type="range" name="slider5" min="0" max="10">
                </div>
            </div>
          
            <a  ></a>
        </div>
        </form>
    </body>
    </html>
    
    

    Icon:

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 4, 2016 8:50 AM
  • User541108374 posted

    Hi,

    create a Web API which I find difficult to understand on this subject

    I suggest you take the tutorial http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api to get the bare concepts of it.

    Later I will use my embedded linuc (C++) terminal to consume it.

    It's been quite some time I did something with c++ but there's a REST SDK which you want to check out: Bringing RESTful Services to C++ Developers.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 4, 2016 1:13 PM

All replies

  • User541108374 posted

    Hi,

    an embedded machine

    Which language are you going to use on it? C# with mono on a raspberry pi or something else?

    I want to use Web API

    Is the Web API created by you or do you still need to create it or are you going to consume it only?

    Grz, Kris.

    Friday, March 4, 2016 8:11 AM
  • User723941679 posted

    Hi @XIII,

    My first step to create a Web API which I find difficult to understand on this subject. So is it possible to show m ebasic example how to create it?

    Later I will use my embedded linuc (C++) terminal to consume it.

    Kind Regards

    Friday, March 4, 2016 8:19 AM
  • User36583972 posted

    Hi NTMS2016,

    According to your description, I have made a sample on my side. You can try the following code.

    Define two classes in the API, to receive and return data:

    public class SendModel
        {
            public string CustName { set; get; }
            public string CustSurname { set; get; }
            public string Location { set; get; }
    
            public string Amount { set; get; }
    
        }
    
        public class ReceiveModel
        {
            public string CardNo { set; get; }
            public string CardPass { set; get; }
            public string SerialNo { set; get; }
        }

    In ValuesController, we are increasing the Post method:

    public class ValuesController : ApiController
        {
    
            // GET api/values
            [AllowAnonymous]
            public IEnumerable<string> Get()
            {
                return new string[] { "value1", "value2" };
            }
    
            // GET api/values/5
            public string Get(int id)
            {
                return "value";
            }
    
            // POST api/values
            [AllowAnonymous]
            public SendModel Post([FromBody]ReceiveModel receiveModel)
            {
                string serialNo = receiveModel.SerialNo;
                    SendModel sd = new SendModel() { Amount = "1", CustName = "name", CustSurname = "hello ", Location = "Yoko" };
                   return sd;
            }
    
            // PUT api/values/5
            public void Put(int id, [FromBody]string value)
            {
            }
    
            // DELETE api/values/5
            public void Delete(int id)
            {
            }
        }
    

    Finally, we call on the page with Ajax.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="Scripts/jquery-1.10.2.js"></script>
        <script src="http://code.angularjs.org/1.1.5/angular.min.js"></script>
        <script>
            $(document).ready(function () {
                $('#btnmethod').click(function () {
                    var userWeightings = [];
                    $('#sliders input[type=range]').each(function (i, obj) {
                        var sliderName = $(obj).attr('name');
                        var sliderValue = $(obj).val();
                        var userWeighting = { RuleName: sliderName, Weighting: sliderValue };
                        userWeightings.push(userWeighting);
                    })
                    var str = JSON.stringify(userWeightings);
                    alert(str);
                    $.ajax({
                        url: "http://localhost:3057/api/Values",
                        type: "Post",
                        contentType: "application/json; charset=urf-8",
                        dataType: "json",
                        //data: "{ RuleName:'slider1',Weighting:200}",
                        //data: str,
                        data: "{'CardNo':'001','CardPass':'XX002','SerialNo':'B6678'}",
                        success: function (response) {
                            alert(JSON.stringify(response))
                        }, 
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert("error-Put:" + textStatus + "  " + errorThrown);
                        }
                    });
                });
            });
        </script>
    
    
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:TextBox ID="TextBox1" runat="server" Height="31px" Width="417px"></asp:TextBox>
            <br />
            <asp:Button ID="btnmethod" runat="server" Text="Method" />
            <br />
            <br />
            <div>
            </div>
            <br />
            <div id="sliders">
                <div>
                    <input type="range" name="slider1" min="0" max="10">
                </div>
                <div>
                    <input type="range" name="slider2" min="0" max="10">
                </div>
                <div>
                    <input type="range" name="slider3" min="0" max="10">
                </div>
                <div>
                    <input type="range" name="slider4" min="0" max="10">
                </div>
                <div>
                    <input type="range" name="slider5" min="0" max="10">
                </div>
            </div>
          
            <a  ></a>
        </div>
        </form>
    </body>
    </html>
    
    

    Icon:

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 4, 2016 8:50 AM
  • User723941679 posted

    Hi @Yohann Lu

    Thank you for info. I am bit confuse.

    In my embedded device I have to send ReceiveModel data into Web API. Am I right? So how can I do that?

    Once I get data help of ReceiveModel class I have to send the values in my SQL SP and receive data so I can sent the info back to the embedded device.

    In your code you use public SendModel Post([FromBody]ReceiveModel receiveModel) for this. But I dont understand why you use [FromBody]?

    I want to use web services for my project but it seems much easier to use web api for single communication and less security configuration.

    So in your basic example you use serial no and get the information before post. Now I need some missing part. How to send the serial no to SQL SP and get data, analyse it and make ready for public SendModel Post.

    Could it be possible to show me the missing part.

    Kind Regards

    Friday, March 4, 2016 9:14 AM
  • User541108374 posted

    Hi,

    create a Web API which I find difficult to understand on this subject

    I suggest you take the tutorial http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api to get the bare concepts of it.

    Later I will use my embedded linuc (C++) terminal to consume it.

    It's been quite some time I did something with c++ but there's a REST SDK which you want to check out: Bringing RESTful Services to C++ Developers.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 4, 2016 1:13 PM
  • User-1135002761 posted

    Hi NTMS2016,

                            You can follow this site for WebAPI using AngularJS

    https://techdobby.wordpress.com/2016/02/17/single-page-aplication-in-angularjs-with-bootstrap/

    Friday, March 4, 2016 2:39 PM
  • User723941679 posted

    Hi @XIII,

    I will check it, thank you.

    Friday, March 4, 2016 3:22 PM