locked
Post Data RRS feed

  • Question

  • User1979860870 posted

    Hi

      How to Post Data using RestSharp in Web Api and return message to Client 'Successfully Added Emp Id : 10'.

    Like below

    public HttpResponseMessage Post([FromBody]JToken postData, HttpRequestMessage request)
            {

    }

    public class PMDbContext : DbContext
        {
            public DbSet<Department> Departments { get; set; }
            public DbSet<Employee> Employees { get; set; }
        }

    Thanks

    Tuesday, February 9, 2021 9:53 AM

All replies

  • User2041008840 posted

    There are bunch of examples on restSharp you should search on google 
    btw, 
    here is the example

    var client = new RestClient("http://api.dastanito.ir");
    var request = new RestRequest("/storiesmaster/creat.php", Method.POST);
    
    request.AddJsonBody(new
    {
        story_code = "value",
        master_code = "value2"
    });
    
    IRestResponse response = client.Execute(request);
    var content = response.Content; // {"message":" created."}



    or

    public TResponse Post<TResponse>(string relativeUri, object postBody) where TResponse : new()
        {
            //Note: Ideally the RestClient isn't created for each request. 
            var restClient = new RestClient("http://localhost:999");
    
            var restRequest = new RestRequest(relativeUri, Method.POST)
            {
                RequestFormat = DataFormat.Json
            };
    
            restRequest.AddBody(postBody);
    
            var result = restClient.Post<TResponse>(restRequest);
    
            if (!result.IsSuccessful)
            {
                throw new HttpException($"Item not found: {result.ErrorMessage}");
            }
    
            return result.Data;
        }

    Tuesday, February 9, 2021 10:09 AM
  • User1979860870 posted

    Hi Prathamesh

       What should be the HttpPost code in Api

    public class Employee
        {
            public Employee()
            {
                CreatedOn  = DateTime.Now;
            }
            public string Id { get; set; }
            public string Name { get; set; }
            public string Address { get; set; }
            public string Gender { get; set; }
            public string Designation { get; set; }
    
            [Display(Name = "Date Of Birth")]
            [DataType(DataType.Date)]
            [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
            public DateTime Dob { get; set; }
    
    
            public virtual Department Departments { get; set; }
        }

    Thanks

    Tuesday, February 9, 2021 10:15 AM
  • User2041008840 posted

    [HttpPost] used in API 
    to post/insert data into database or to post any value from front end 


    Monday, February 15, 2021 8:41 AM