I've recently started using WCF WebApi to create a REST api. I followed samples available on CodePlex and also articles series by Alex Zeitler.
I tried to create a method which accepts data via POST as follows:
[ServiceContract]
public class AuthenticateApi
{
[WebInvoke(UriTemplate = "", Method = "POST")]
public HttpResponseMessage<LoginModel> Post(LoginModel loginModel)
{
loginModel.IsValidated = true;
return new HttpResponseMessage<LoginModel>(loginModel);
}
}
And this is my Entity:
public class LoginModel
{
public string Username { get; set; }
public string Password { get; set; }
public bool IsValidated { get; set; }
}
And finally this is my configuration in Global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapServiceRoute<AuthenticateApi>("login");
}
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
When I try to POST something using Fiddler this way:
Content-Type: application/json
Accept: application/json
{"Username": "mahdi", "Password":"123"}
Host: localhost:8181
I receive the following error message:
The server encountered an error processing the request. The exception message is 'Specified value has invalid HTTP Header characters. Parameter name: name'. See server logs for more details. The exception stack trace is:
at System.Net.WebHeaderCollection.CheckBadChars(String name, Boolean isHeaderValue) at System.Net.WebH开发者_如何学运维eaderCollection.Add(String name, String value) at System.Collections.Specialized.NameValueCollection.Add(NameValueCollection c) at System.ServiceModel.Activation.HostedHttpContext.HostedRequestContainer.System.ServiceModel.Channels.HttpRequestMessageProperty.IHttpHeaderProvider.CopyHeaders(WebHeaderCollection headers) at System.ServiceModel.Channels.HttpRequestMessageProperty.get_Headers() at Microsoft.ApplicationServer.Http.Channels.HttpMessageEncodingRequestContext.ConfigureRequestMessage(Message message) in F:\codeplex\wcf\Http\Src\Microsoft.ApplicationServer.Http\Microsoft\ApplicationServer\Http\Channels\HttpMessageEncodingRequestContext.cs:line 222 at Microsoft.ApplicationServer.Http.Channels.HttpMessageEncodingRequestContext.get_RequestMessage() in F:\codeplex\wcf\Http\Src\Microsoft.ApplicationServer.Http\Microsoft\ApplicationServer\Http\Channels\HttpMessageEncodingRequestContext.cs:line 54 at System.ServiceModel.Dispatcher.ChannelHandler.EnsureChannelAndEndpoint(RequestContext request) at System.ServiceModel.Dispatcher.ChannelHandler.TryRetrievingInstanceContext(RequestContext request)
Any idea why this happens?
Put the JSON object in the request body field, not in with the headers.
精彩评论