REST API Post request - Request Error - wcf

I have a REST API call that uses post method. The WCF Service inturn saves the input string to the database. Here is my WCF Contract
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, UriTemplate = "/xxxx")]
int xxxx(string username);
When I use DHC client on chrome to test , it displays a bad request error. The json data send to the server is of the form {"username": test123}
Pls find the image below
Is the format of the data sent to server incorrect ? Please suggest.

The contract should be marked with following WebInvoke attributes -
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "/xxxx")]

Related

How to do versioning in WCF services

I am using WCF services for the Mobile and Web platform and I want to apply the versions to them included Database version as well.
[OperationContract]
[FaultContract(typeof(CommonFaultContract))]
[WebInvoke(Method = "GET", UriTemplate = "/login/{uname},{password},{uuid},{deviceName},{deviceOS}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
UserDataContract login(string uname, string password, string uuid, string deviceName, string deviceOS);
using the above code to call the operation contact
These links may help you-
msdn data contract versioning
wcf handle versioning

WCF REST error HTTP 307

I have a REST WCF service. When a POST attempt is made to this service to an operation as mentioned below, I get the below error:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "", RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
void Write();
Error:
HTTP 307
There is no operation listening for https://xx.xxx.xx.xxx/EnrollmentServer/Discovery.svc, but there is an operation listening for https://xx.xxx.xx.xxx/EnrollmentServer/Discovery.svc/, so you are being redirected there.
Any pointers will be appreciated.
Change your UriTemplate to "/" instead. RESTful WCF has a "thing" about wanting Uris ending with / instead of no ending.
Also, use the Uri with the ending slash to avoid WCF responding with the 307.

Why WCF service is getting old values of parameters

I have setup two projects. One is WCF service project with following contract:
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "xml/{filterName}/{filterValue}")]
XmlElement XMLDataWithFilter(string filterName, string filterValue);
Other project uses this WCF service. The values for the parameters filterName and filterValue sent with first call remain same when some I make call to WCF service with some other parameters.
Why WCF service is retaining the values of parameters?

How to use XmlSerializerFormat in a Restful Webservice?

We have created a WCF webservice. We have created two operation contracts like this:
public interface IRestServiceImpl
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "getID/{id}")]
File getID(string id);
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Xml,
RequestFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "auth")]
ResponseData Auth(RequestData rData); }
As you can see I have a Get and Post method. This works fine, but if I want to set the response format of my GET service,I have to use XMLSERIALIZERFORMAT tag.If I add this and then try to hit POST service, I get Error:400 "Bad Request Error". How should I set the response of my GET method in the same service.
I have tried adding another service 'Service2.svc' in the project where I have kept only GET methods and 'Service1.svc' has only POST services. In the interface of Service2, i have used XMLSERIALIZERFORMAT but still not able to hit POST service. I think I have to use DATACONTRACTSERIALIZER. But I dont know how to set the attributes in that.
Can anyone please help me out with this?
Thanks
Charan
If you want your GET to send a Response in XML then below setting on your GET method should be enough.
ResponseFormat = WebMessageFormat.Xml
You dont need to use XmlSerializerFormat. Just try to browse to your service in IE and you should see the response from your GET Method would be in XML format.
Regarding getting a Bad Request on your POST try to see on how the request body is being sent to your method. To see your request body try using tools like Fiddler to inspect your request and response. To debug the cause of Bad Request try enabling tracing on your service.
How to enable tracing on your Service

WCF WebInvoke ResponseFormat

I have a WCF restul service and I want to allow the user to choose what request format they want, i have the decorations
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "getstreamurl?ch={ch}&format=xml")]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "getstreamurl?ch={ch}&format=json")]
First of, is there a way to specify the ResponseFormat at runtime and take the format in as an argument to the method? From reading around i dont think so... OK next thing
The above code is ok and works, but im having a problem, i want to be able to specify a default, so when no format arguement is passed then i just default but if i decorate like so
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "getstreamurl?ch={ch})]
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "getstreamurl?ch={ch}&format=json")]
Where the XML is the default, if i try to call the service method through the browser it tells me that:
UriTemplateTable does not support multiple templates that have equivalent path as template 'getstreamurl?ch={ch}' but have different query strings, where the query strings cannot all be disambiguated via literal values. See the documentation for UriTemplateTable for more detail
They obviously can be distinguished but it seems that WCF is only reading up to the argument and thats it...Any suggestions?
No, I don't think you can do that programmatically at runtime. What you can do of course if to expose two distinct endpoints from your service - one returning XML, another returning JSON, and then programmatically pick which one to call from your client app.
Marc
Update: as Steve Michelotti correctly points out, this automatic switching between JSON and XML can now be achieved in WCF 4.0. WCF 4.0 has an improved REST support which also includes an Format Message Selection feature, based on HTTP accept headers.
For more info on WCF 4.0's new features, see: A Developer's Introduction to WCF 4.0
You can do this if your rest service is configured automatically select response type.
Then on client request simply add needed header Accept: application/json