I'm trying to make an application for Windows which acts like a server for a mobile application (PhoneGap). This application is like a remote for the server application, it invokes methods in which their turn do things.
After long searching and trying to see which components can work together I found OWIN and Web API. So I'm trying to implement this into my application, but I cannot seem to grasp how I can POST a string to invoke methods.
My thought of process is that I POST a string to the server, which it reads and with a switch statement to check the value of the string I know which method to invoke. Very simple, straightforward (not faulty proof probably), but it's a start.
But I cannot seem to get it to work. Here is my controller:
public void Post([FromBody]string value)
{
switch(value)
{
case("buttonOne"):
{
mainClass.pressButtonOne();
break;
}
}
}
I'm using HttpClient to emulate the client on the host pc:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:6740");
var content = "buttonOne";
var result = client.PostAsync("api/values", content).Result;
string resultContent = result.Content.ReadAsStringAsync().Result;
Console.WriteLine(resultContent);
But this is getting errors, the string isn't a valid HttpContent. But it's always asking for a pair instead of single.
1 Answer 1
When making POST requests the post content should be of type HttpContent or one of its derived types.
var content = new FormUrlEncodedContent(new Dictionary<string, string> {
{"value" , "buttonOne"} })
var content = new FormUrlEncodedContent(new Dictionary<string, string> { {"value" , "buttonOne"} })