0

Hopefully a simple question, but I have a simple app that is self-hosted (which works fine), and I can hit the requested method using a POST without parameters from the body (which hits the method but defaults the parameter to null), but when I try to pass the named parameter in the BODY of the POST request in raw JSON format, it receives a 400 response, and never hits the method for some reason...

Any advice is appreciated.

[Environment: Visual Studio 2015, C#, self hosted REST application]

[Code Details]

(Web service hosting code for Self Hosted app)

WebServiceHost host = new WebServiceHost(typeof(CalculatorServiceREST), new Uri("http://localhost:8000"));
ServiceEndpoint ep = host.AddServiceEndpoint(typeof(ICalculator), new WebHttpBinding(), "");
ServiceDebugBehavior stp = host.Description.Behaviors.Find<ServiceDebugBehavior>();
stp.HttpHelpPageEnabled = false;
host.Open();
Console.WriteLine("Service is up and running");
Console.WriteLine("Press enter to quit ");
Console.ReadLine();
host.Close();

(The contracts implementation class: CalculatorServiceRest.cs)

public class CalculatorServiceREST : ICalculator
{
    [WebInvoke(Method = "POST")]   // POST to:  /RoundUp (with BODY item of 'number' in the request)
    public int RoundUp(double number)
    {
        return Convert.ToInt32(Math.Round(number));
    }

    [HttpPost]   // POST to:  /GetName (with BODY item of 'number' in the request)
    public string GetName(string name)
    {
        return name;
    }
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
CodingRiot
  • 197
  • 1
  • 4
  • 16

1 Answers1

0

It appears that I have to add the following attribute values to the POST method so that I can pass JSON data in the BODY for it to pick it up (which is different from how I am use to being able to just put [HttpPost] attribute for MVC and it just knows how to pick it up. Can anyone provide some insight on why it is different?

   [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]   
CodingRiot
  • 197
  • 1
  • 4
  • 16