1

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.

asked Feb 8, 2015 at 21:40
1
  • You're not passing the content correctly. Try this var content = new FormUrlEncodedContent(new Dictionary<string, string> { {"value" , "buttonOne"} }) Commented Feb 8, 2015 at 22:23

1 Answer 1

2

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"} })
answered Feb 8, 2015 at 22:49
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.