0

I have created a method of get type with class type parameter in web api (.NET MVC).Like below code

class Employee
{
   int Ind;
   int Empno;
}

[HttpGet]
public DataTable GetEmployee(Employee e)
{
   .............
   .............
   
   return new DataTable();
}

Below code I am using to call API:

internal DataTable CallApi()
{
     var client = new RestClient("......./Ctrl/GetEmployee");
     client.Timeout = -1;
     var request = new RestRequest(Method.GET);
     request.AddHeader("Content-Type", "application/json");
     Employee emp = new Employee()
     {
        Ind = 1,
        Empno = 10001
     };
     var body = JsonConvert.SerializeObject(emp);
     request.AddParameter("application/json", body, ParameterType.RequestBody);
     IRestResponse response = client.Execute(request);

     Console.WriteLine(response.Content);

     DataTable dt = new DataTable();
     dt = JsonConvert.DeserializeObject<DataTable>(response.Content);
     return dt;
}

I want to call above API method from ASP.NET project, Abobe code is hitting to api method, But problem is that Parameter that I am sending to call api method is not getting in api method parameter,

In objcet Employee e , Parameter is not getting the sent objct.

  • @Luke That doesn't change the fact that `GET` requests generally don't have request-bodies, and while it _is_ supported by many systems, equally many systems in-between everyone else (think: web security firewalls, corporate stuff, etc) will complain: also, the latest HTTP spec implies it shouldn't be supported: https://stackoverflow.com/questions/978061/http-get-with-request-body – Dai Oct 11 '22 at 10:06
  • I have also checked it : ` GetEmployee([FromBody] Employee e)` but I am getting same problem – Vivekanand Vishvkarma Oct 11 '22 at 10:06
  • ...why does an action named `GetEmployee` need an `Employee` DTO to be *passed-in*? That makes no sense... – Dai Oct 11 '22 at 10:07
  • @Dai you're right, I'm not fully awake – Luke Oct 11 '22 at 10:10
  • When I am calling API from Postman then working properly – Vivekanand Vishvkarma Oct 11 '22 at 10:13

0 Answers0