Trying to call POST method with own data type, media type and codec leads to MethodNotAllowed

36 views
Skip to first unread message

Alexey Gravanov

unread,
Jan 12, 2012, 3:15:14 AM1/12/12
to OpenRasta
Hi everyone,

I'm trying to develop some REST service using OpenRasta and faced
problem that I can't figure out how to configure the whole thing to
make POSTs possible while using my own data type, media type and
codec. For now, I've just copied JsonDataContractCodec and named it
TAFCodec.

Here is the code:

[Serializable]
public class Configuration : IConfigurationSource
{
public void Configure()
{
using (OpenRastaConfiguration.Manual)
{
ResourceSpace.Has.ResourcesOfType<TAFResource>()
.AtUri("/msg/gwp/taf")
.HandledBy<TAFHandler>()
.TranscodedBy<TAFCodec>().ForMediaType("application/
vnd.as24.message.taf+json");
}
}
}

public class TAFHandler
{
[HttpOperation(HttpMethod.GET)]
public object Get()
{
return new OperationResult.OK
{
ResponseResource = new TAFResource();
};
}

[HttpOperation(HttpMethod.POST)]
public object Post(TAFResource resource)
{
return new OperationResult.Created
{
ResponseResource = resource
};
}
}

If I'm calling GET method using RestSharp.RestRequest, it works fine
and I'm getting correct data back:

var request = new RestRequest
{
Resource = "/message/gwp/taf",
Method = Method.GET
};
request.AddHeader("Accept", "application/
vnd.as24.message.taf+json");
var response = client.Execute(request);

But if I'm trying to call POST method:

var request = new RestRequest
{
Resource = "/message/gwp/taf",
Method = Method.POST
};
request.AddHeader("Accept", "application/
vnd.as24.message.taf+json");
var stream = new MemoryStream();
new
DataContractJsonSerializer(typeof(TAFResource)).WriteObject(stream,
new TAFResource());
request.AddParameter("application/vnd.as24.message.taf
+json", stream.GetBuffer(), ParameterType.RequestBody);
var response = client.Execute(request);

I'm getting back MethodNotAllowed.

I've tried to put breakpoint in OpenRasta's
HttpListenerHost.ProcessRequest and look what is in request, but it
looks quite good, there is my accept header, my "application/
vnd.as24.message.taf+json" media type in request's content type and so
on.

Any ideas what's happening and what should be changed to make POST
work as well?

Thanks in advance.. ;-)
Reply all
Reply to author
Forward
0 new messages