1

I have a task where i need to request web api GET request with complex type parameter, I guess we don't be able to do such thing as GET request expects everything to be shared through URL.

can anyone help me on how to achieve this. Consuming Web API GET request with JSON data through C#.

Consumer Console:

class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // Need to pass this through GET Request
                var employee = new Employee() { EmployeeId = 1, EmployeeName = "Test", Designation = "Developer", Salary = 100 };
                var jsonParam = JsonConvert.SerializeObject(employee);
                //


                var request = (HttpWebRequest)WebRequest.Create("http://localhost:52237/Values/GetEmp");                

                var encoding = new UTF8Encoding();
                var bytes = encoding.GetBytes(jsonParam);

                request.Method = "GET";
                request.ContentLength = bytes.Length;
                request.ContentType = "application/json";

                using (var writeStream = request.GetRequestStream())
                {
                    writeStream.Write(bytes, 0, bytes.Length);
                }

                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    var responseValue = string.Empty;

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        // grab the response
                        using (var responseStream = response.GetResponseStream())
                        {
                            if (responseStream != null)
                                using (var reader = new StreamReader(responseStream))
                                {
                                    responseValue = reader.ReadToEnd();
                                }
                        }
                    }
                }              
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
    public class Employee
    {
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public int Salary { get; set; }
        public string Designation { get; set; }
    }

Web API:

public class ValuesController : ApiController
    {        
        [HttpGet]
        [Route("api/GetEmp")]
        public Employee GetEmp([FromUri]Employee employee)
        {
            // Getting employee object from client

            // Yet to implement

            if (employee != null)
            {
                employee.Designation = "Engineer";
            }
            return employee;
        }
    }

    public class Employee
    {
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public int Salary { get; set; }
        public string Designation { get; set; }
    }

Thanks in Advance.

Vicky S
  • 762
  • 4
  • 16
  • These links may be help you https://stackoverflow.com/questions/29571284/for-restful-api-can-get-method-use-json-data and https://stackoverflow.com/questions/50850318/rest-api-passing-json-string-as-parameter-value – Masoud Sadeghi Jul 23 '19 at 04:47
  • Add your complex type in question or sample request format – Md Farid Uddin Kiron Jul 23 '19 at 04:51
  • @MdFaridUddinKiron updated my question with the code what i have tried – Vicky S Jul 23 '19 at 05:18
  • Side note 1: complex types in GET parameters are **always** a bad idea and an architecture mistake. Side note 2: [MS recomends](https://learn.microsoft.com/en-us/dotnet/api/system.net.webrequest?view=netframework-4.8) to use [HttpClient](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.8) instead of `WebRequest`. – vasily.sib Jul 23 '19 at 05:32
  • Would you like to request in same format or I would customize it – Md Farid Uddin Kiron Jul 23 '19 at 05:43
  • i would like to have it in same format, will be fine if you customize – Vicky S Jul 23 '19 at 05:53
  • Why not just get the employee from employee id as a parameter which makes sense in using a GET request? If there's validation issue, you would check that when retrieved? unless I'm missing something? – Rav Jul 23 '19 at 16:05

1 Answers1

2

I think after HTTP 1.1 version you can also send data in body in GET request. so instead of [FromUri] you can use [FromBody].

    [HttpGet]
    [Route("api/GetEmp")]
    public Employee GetEmp([FromBody]Employee employee)
    {
        // Getting employee object from client

        // Yet to implement

        if (employee != null)
        {
            employee.Designation = "Engineer";
        }
        return employee;
    }