locked
How to Post Request in web form RRS feed

  • Question

  • User-1971168174 posted

    Hi ,

    I am using restlet client chrome extension to post fields to API,its working fine there and i am getting sucess msg, but i have to do the same on asp.net web form to post the fields.

    Below is the sample content which works fine on posting at restlet client , Need help to have sample working code (asp.net c# web form )  to post below data format to rest API  

    Body as below

    {

      "Tablename": "transaction ",

      "User_Id": "user@xyz.com",

      "Password": "admin123",

      "Table_Data": [

        {   

    "status": "New",

    "email ": " user@xyz.com ",

    "date ": "8/6/2019 7:10:28 AM",

    "empid": "464",

    "rollno": "989",

    "count": "90"

        }

      ]

    }

     

     Under Header as below

    Content-Type-application/json

    Schema –production

    Method- Post

    Api- http://newapi/newmethod/new

     

    Output –

    {

    "success": true,

    "message": "GOOD",

    "data": null

    }

     Thanks in advance !

    Sunday, September 29, 2019 9:24 PM

Answers

  • User-1971168174 posted

    Need your help to 

    1.  pass schema as xbar in header ,

    2.  also if you can help me in creating object to be passed in data here in below json format, i tried but its not parsing in the below format

    {

      "User_Id": "user@xyz.com",

      "Password": "admin123",

      "Table_Data": [

        {   

    "status": "New",

    "email ": " user@xyz.com ",

    "date ": "8/6/2019 7:10:28 AM"

        }

      ]

    }

    I tried below code but its not formatting data in the above format

    <div>

    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net.Http;
    using System.Web.Script.Serialization;
    
    namespace KeeyRequest
    {
        public partial class test : System.Web.UI.Page
        {
            public class Keey
            {
                public string User_Id{ get; set; }           
                public string Password { get; set; }
    
                public IList<string> Table_Data { get; set; }
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                Keey account = new Keey
                {
                   
                    User_Id = "xyz@gmail.com",
                    Password = "passnew",
                    //  CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc),
                    Table_Data = new List<string>
        {
                        "status:New",
                        "email:xyz@gmail.com",
                        "date:8/6/2019 7:10:28 AM"
    
    
        }
                };
    
                HttpClient client = new HttpClient();
                var json = new JavaScriptSerializer().Serialize(account);            
                var result = client.PostAsync("http://app/xyz", new StringContent(
                    JsonConvert.SerializeObject(account),
                    Encoding.UTF8,
                    "application/json")
                    ).Result;
    
                var data = result.Content.ReadAsStringAsync().Result;
            }
        }
    }

    </div>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 30, 2019 9:03 AM
  • User-474980206 posted

    because you defined Table_Data as a list of 3 strings, in Json it becomes an array of 3 strings. In the desired you show it as an array of 1 object.  try:

    Table_Data = new List<object>
    {
       new {
         status = "New",
         requestor_email = "xyz@xyz.com",
         start_date = "8/6/2019 7:10:28 AM"
       }
    ]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 30, 2019 10:37 PM

All replies

  • User61956409 posted

    Hi neerajkumarmodi,

    Need help to have sample working code (asp.net c# web form )  to post below data format to rest API  

    The following code snippet work well on my side, you can refer to it to achieve the requirement.

    HttpClient client = new HttpClient();
    
    var result = client.PostAsync("{request_url_here}", new StringContent(
        JsonConvert.SerializeObject(your_data_here),
        Encoding.UTF8,
        "application/json")
        ).Result;
    
    var data = result.Content.ReadAsStringAsync().Result;

    With Regards,

    Fei Han

    Monday, September 30, 2019 2:48 AM
  • User-1971168174 posted

    Need your help to 

    1.  pass schema as xbar in header ,

    2.  also if you can help me in creating object to be passed in data here in below json format, i tried but its not parsing in the below format

    {

      "User_Id": "user@xyz.com",

      "Password": "admin123",

      "Table_Data": [

        {   

    "status": "New",

    "email ": " user@xyz.com ",

    "date ": "8/6/2019 7:10:28 AM"

        }

      ]

    }

    I tried below code but its not formatting data in the above format

    <div>

    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net.Http;
    using System.Web.Script.Serialization;
    
    namespace KeeyRequest
    {
        public partial class test : System.Web.UI.Page
        {
            public class Keey
            {
                public string User_Id{ get; set; }           
                public string Password { get; set; }
    
                public IList<string> Table_Data { get; set; }
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                Keey account = new Keey
                {
                   
                    User_Id = "xyz@gmail.com",
                    Password = "passnew",
                    //  CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc),
                    Table_Data = new List<string>
        {
                        "status:New",
                        "email:xyz@gmail.com",
                        "date:8/6/2019 7:10:28 AM"
    
    
        }
                };
    
                HttpClient client = new HttpClient();
                var json = new JavaScriptSerializer().Serialize(account);            
                var result = client.PostAsync("http://app/xyz", new StringContent(
                    JsonConvert.SerializeObject(account),
                    Encoding.UTF8,
                    "application/json")
                    ).Result;
    
                var data = result.Content.ReadAsStringAsync().Result;
            }
        }
    }

    </div>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 30, 2019 9:03 AM
  • User753101303 posted

    Hi,

    You are using a list of strings when it is a list of objects with status, email and date properties. With VS 2017 you can use https://dailydotnettips.com/did-you-know-you-can-automatically-create-classes-from-json-or-xml-in-visual-studio/ to generate strongly typed C# classes from a JSON or XML sample.

    You could try  perhaps anonymous types and something such as:

        Table_Data = new[]
    { new {"status"="New",email="xyz@gmail.com",date=new DateSerial(2019,6,8)} }.ToList();

    though I prefer to use strong types when possible unless I really need them only in a single method...

    Monday, September 30, 2019 1:00 PM
  • User-1971168174 posted

    Hi PatriceSc,

    I need to have following output for posting it,but not able to get that using code sample below:

    Output getting as by code

    {
    "Table_Name":"new_request",
    "User_Id":"xyz@gmail.com",
    "Password":"passnew",

    "Table_Data":[
    "status:New",
    "requestor_email:xyz@xyz.com",
    "start_date:8/6/2019 7:10:28 AM"
    ]
    }

    Desired Output

    {
    "Table_Name":"new_request",
    "User_Id":"xyz@gmail.com",
    "Password":"passnew",

    "Table_Data":[
    { // not coming above
    "status": "New", //coming as  "status:New", above
    "requestor_email":"xyz@xyz.com",
    "start_date":"8/6/2019 7:10:28 AM"
    }// not coming above
    ]
    }

    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net.Http;
    using System.Web.Script.Serialization;
    
    namespace KeeylockoRequest
    {
        public partial class test : System.Web.UI.Page
        {
            public class Keeylocko
            {
                public string Table_Name { get; set; }
                public string User_Id { get; set; }
                public string Password { get; set; }
    
                public IList<string> Table_Data { get; set; }
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                Keeylocko my_data_to_pass = new Keeylocko
                {
                    Table_Name = "new_request",
                    User_Id = "xyz@gmail.com",
                    Password = "passnew",                
                    Table_Data = new List<string>
        {
                        "status:New",
                        "requestor_email:xyz@xyz.com",
                        "start_date:8/6/2019 7:10:28 AM"
    
    
        }
                };
    
                HttpClient client = new HttpClient();
                var json = new JavaScriptSerializer().Serialize(my_data_to_pass);            
                var result = client.PostAsync("http://app/new/api", new StringContent(
                    JsonConvert.SerializeObject(my_data_to_pass),
                    Encoding.UTF8,
                    "application/json")
                    ).Result;
    
                var data = result.Content.ReadAsStringAsync().Result;
            }
        }
    }

    Monday, September 30, 2019 3:56 PM
  • User-474980206 posted

    because you defined Table_Data as a list of 3 strings, in Json it becomes an array of 3 strings. In the desired you show it as an array of 1 object.  try:

    Table_Data = new List<object>
    {
       new {
         status = "New",
         requestor_email = "xyz@xyz.com",
         start_date = "8/6/2019 7:10:28 AM"
       }
    ]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 30, 2019 10:37 PM