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);
}
}