2

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.

7
  • These links may be help you stackoverflow.com/questions/29571284/… and stackoverflow.com/questions/50850318/… Commented Jul 23, 2019 at 4:47
  • Add your complex type in question or sample request format Commented Jul 23, 2019 at 4:51
  • @MdFaridUddinKiron updated my question with the code what i have tried Commented Jul 23, 2019 at 5:18
  • Side note 1: complex types in GET parameters are always a bad idea and an architecture mistake. Side note 2: MS recomends to use HttpClient instead of WebRequest. Commented Jul 23, 2019 at 5:32
  • Would you like to request in same format or I would customize it Commented Jul 23, 2019 at 5:43

1 Answer 1

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;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

can you please share how to consume this through console app

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.