-1

How can I consume Web API 2 GET command in C# that accepts one FromUri parameter and 2nd FromBody parameter. I dont know how to send body in GET command, do i need to use POST command? but how? below is the code I have written so far. Thank you.

API Code

[HttpGet]
[ResponseType(typeof(IEnumerable<Student>))]
public IHttpActionResult Find([FromUri]string searchText,[FromBody]SearchType searchType)
{
    //EF code to get data from DB
    using (handler)
    {
        return Ok(handler.Find(searchText, searchType));
    }

}

HttpClient Code

static void Main(string[] args)
{

     HttpClient client = new HttpClient();
     client.BaseAddress = new Uri("http://localhost:55587/");
     client.DefaultRequestHeaders.Accept.Clear();
     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

     string aSearchText ="John";
     SearchType aSearchType = SearchType.Name; //this is enum

     Task<HttpResponseMessage> responseTask = client.GetAsync($"api/Student/{aSearchText}");
     responseTask.Wait();

     ////////////////////
     /// Code missing how to send "aSearchType" as a body in Get Command?
     ////////////////////

     var ListTask = responseTask.Content.ReadAsAsync<IEnumerable<Student>>();
     ListTask.Wait();

     IEnumerable<Student> list = ListTask.Result;

     foreach(Student s in list)
     {
         Console.WriteLine(s.Name);
     }
}
Abubakar Riaz
  • 320
  • 8
  • 26
  • https://stackoverflow.com/questions/43421126/possible-for-httpclient-to-send-content-or-body-for-get-request – xdtTransform Jul 22 '19 at 12:06
  • And I will recommend reading [RFC 2616 Sc9.3](https://tools.ietf.org/html/rfc2616#section-9.3). It's only a recommendation sure. The spec doens't said you can not. It said you should not. – xdtTransform Jul 22 '19 at 12:12

1 Answers1

0

you can pass the parameter from body to HttpGet actions but it is not logical.

you can use HttpPost to send data from the body.

[HttpPost]
[ResponseType(typeof(IEnumerable<Student>))]
public IHttpActionResult Find([FromBody]SearchType searchType,[FromUri]string searchText)
{
    //EF code to get data from DB
    using (handler)
    {
        return Ok(handler.Find(searchText, searchType));
    }

}
string aSearchText ="John";
     SearchType aSearchType = SearchType.Name; //this is enum

     Task<HttpResponseMessage> responseTask = client.PostAsJsonAsync($"api/Student/{aSearchText}",aSearchType );
     responseTask.Wait();
Farhad Zamani
  • 5,381
  • 2
  • 16
  • 41
  • "you can not pass the parameter from body to HttpGet actions.". In fact you can. Spec doesn't banned. It just advice that you don't. I believe that Elastic search use this principle for big query. Any http method can have a message body. – xdtTransform Jul 22 '19 at 12:18
  • @xdtTransform how do you can send data from body to get method? – Farhad Zamani Jul 22 '19 at 12:21
  • https://stackoverflow.com/questions/43421126/possible-for-httpclient-to-send-content-or-body-for-get-request – xdtTransform Jul 22 '19 at 12:22
  • @xdtTransform Thank you for your guidance. You can do anything in programming! but this is not logical to send data from body in httpget – Farhad Zamani Jul 22 '19 at 12:29
  • Even Roy. Fielding (Aka the guyz that get to sign first every RFC on Http and Rest) advocate that we should not. So I agree with you. He should use http Post. – xdtTransform Jul 22 '19 at 12:33
  • @xdtTransform exactly – Farhad Zamani Jul 22 '19 at 12:43
  • System.Net.Http.WinHttpHandler can send body in a GET request in .Net Framework and .Net Core supports its by default, but since the arguments you guys given i will go for POST command. – Abubakar Riaz Jul 23 '19 at 05:03