REST service does not get POST payload - wcf

I have a big problem.
I created a WCF service.My POST declaration looks like this:
[OperationContract]
[WebInvoke(UriTemplate = "json/put",
Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string PutData(string jsonText);
I was expecting that when I'm sending data (I'm using fiddler2 to test it) that it will automaticly "put" into the jsonText variable.
The service works, but there is no data :(.
Can anybody help? The whole project

WCF expects the JSON payload to be deserialized into a type. Try creating a class that is shaped like your JSON payload and use that as the parameter type.

Related

WCF Service not restfull

My first WEB servicee usinf Visual Studio 2013 Express WEB, and I can't make it restfull. basically want to return a json string and got the following service definition;
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json, UriTemplate = "Name")]
string Name();
Implementation of Name just returns a string which is shown when I Invoke the function through the WCF Test Client, but calling the http://localhost:58116/Service1.svc/Name returns nothing. I was expecting a json representation of the Name.
Any idea's?

Multiple request body error

Actual error on on client side while applying client behavior :
Operation 'GetCurrentStreamPositionByStreamId' of contract
'IVideoService' specifies multiple request body parameters to be
serialized without any wrapper elements. At most one body parameter
can be serialized without wrapper elements. Either remove the extra
body parameters or set the BodyStyle property on the
WebGetAttribute/WebInvokeAttribute to Wrapped.
Specify uritemplate in webinvoke attribute over the method as:
[OperationContract]
[WebInvoke(
Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "getcurrenttime/{managerid}/{streamid}")]
DateTime GetRecordedVideoCurrentTime(string managerid,string streamid);

Wcf service RESTful

I made my method with post like this:
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
List<Human> GetHuman(UserEnteredName humanName);
The UserEnteredName class has just one property - string.
And it works. But, I need to make it to be get, not post.
I tried with this:
[WebInvoke(Method= "GET", UriTemplate = "GetHuman?username={John}",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
But it doesn't work. What do I need to change?
According to your UriTemplate, your method would have to look something like
Human GetHuman(string John)
I suspect you are mistakenly putting a possible parameter value in your UriTemplate. Try something like
[WebInvoke(Method= "GET", UriTemplate = "GetHuman?username={userName}",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
Human GetHuman(string userName)
Also, for GET, you can use the WebGetAttribute, which is slightly cleaner.
I would change your method to take a string parameter and construct the UserEnteredName instance in the method body. It may be possible to use your UserEnteredName type as a parameter if it uses the TypeConverterAttribute, but I have never done this, so I can't say how easy (or not) it is. See the WCF Web HTTP Programming Model Overview, specifically the UriTemplate Query String Parameters and URLs section.

Changing JSON response root node message in WCF

I created a REST GetTime Service in WCF and the service returns JSON as the response message. Also the WebMessageBodyStyle is set to wrapped so it would have an ID associated with that data it returns. But when I use Fiddler to test my service the response string is:
{"GetTimeResult":"2010614104013"}
As the response above the ID of the string is GetTimeResult, I'm wondering is there any way on changing that bit of test to timestamp. So it looks like this:
{"timestamp":"2010614104013"}
Cheers.
If you are using the DataContract/DataMember attributes in your code, you add a name (as well as some other named parameters).
[DataMember(Name = "timestamp")]
public string GetTimeResult
As pointed out in this article, suppose you are not using a data member explicitly in a data contract and want to return, say, a timestamp as a simple string for the response. Just use the annotation [return: MessageParameter(Name = "timestamp")] with your operation contract method:
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/timestamps", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
[return: MessageParameter(Name = "timestamp")]
string GetStringTimestamp();

Period in RESTful WCF Starter Kit

I am using RESTful WFC's WebInvoke and WebGet attribute classes. In the UriTemplate parameter I want to capture a variable which includes the period character. How do I do so?
So
[WebInvoke(Method = "PUT", UriTemplate = "{id}")]
[OperationContract]
TItem UpdateItemInXml(string id, TItem newValue);
I would like it to match the url:
http://service.svc/A.1
I couldn't find a way to do so. The solution I implemented was to base64 encode the id in the URL.