How to prevent WCF service from wrapping CDATA in response? - c#

I have created WCF method which return C# object as response. Now the problem is that while I am posting request to this method using chrome postman I am getting JSON response and some of parameters are wrapped with CDATA. For example, One of parameter URL has value like "http://www.examplewebsite.com/CDATA[SomeValue".
Any solution to prevent wrapping of CDATA ? Or wrapping this URL value like "CDATA[http://www.examplewebsite.com/SomeValue]" ?

Related

Swagger API C#: XML example cannot be generate on Request parameter content-type application/XML

I very new working on Swagger API in C# and I am struggling with generating an XML example for the list object as a Request parameter for application/xml. I need some suggestion or help in my code.
I am using a POST verb which will accept an LIST object and on the Swagger UI side when I click on "Try it Out" button and change the content type to application/xml it gives me an error like this
"<XML example cannot be generated -->'

Using fiddler to test my WEB Api . Which content type to select and the input parameter is always null

I created a Web API which accepts Json string and returns an XML.
I am trying to test my web API using fiddler and unable to test it.
My get method in code:
[HttpGet]
public XmlDocument GetXML([FromBody]string JsonString)
{
System.Xml.XmlDocument xmlDocument = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(JsonString);
In fiddler :When I provide the content type as application json(below is screen shot). It throws a HTTP 500 error.
"An error has occurred.No MediaTypeFormatter is available to read an object of type 'String' from content with media type 'application/json'."
But when I provide the content-Type :application/xml. It successfully makes a connection to the web api but the Input parameter "JsonString" is null.
From your screenshot though it looks like you ARE in fact passing a string because it start with an equals and quotes like ="{ ... }". This looks like a JSONP body. JSON would look start with curl braces like { ... }
If you need to accept JSONP have a look at this answer:
JSONP with ASP.NET Web API

How to get a raw json as a parameter

I wanna get a raw json string from my client in my WebAPI application.
I tried it like this :
public string Get([FromBody]string rawjson)
{
}
I use Google Chrome Rest Console to try my methods first. I add my json content to RAW body and then send a get request.I put a breakpoint on my method to see if i can get the raw json data however, method invokes but rawjson comes as null. I've also tried put but that didn't work either.
What am i doing wrong ?
Thanks
GET methods cannot have Request Body and as such cannot parse values using [FromBody]. Please use the POST method.
Change the rawJson parameter type to Stream you will recieve anything that is posted on your service as as stream and than you can just read that stream to string

How to access the HTTP request body using RestSharp?

I'm building a RESTful API client using C# .NET 3.5.
I first started building it with the good old HttpWebClient (and HttpWebResponse), I could do whatever I wanted with. I were happy. The only thing I stumbled upon was the automatic deserialization from JSON response.
So, I've heard about a wonderful library called RestSharp (104.1) which eases the development of RESTful API clients, and automatically deserialize JSON and XML responses. I switched all my code on it, but now I realize I can't do things I could do with HttpWebClient and HttpWebResponse, like access and edit the raw request body.
Anyone has a solution?
Edit: I know how to set the request body (with request.AddBody()), my problem is that I want to get this request body string, edit it, and re-set it in the request (in other words: updating the request body on the fly)
The request body is a type of parameter. To add one, you can do one of these...
req.AddBody(body);
req.AddBody(body, xmlNamespace);
req.AddParameter("text/xml", body, ParameterType.RequestBody);
req.AddParameter("application/json", body, ParameterType.RequestBody);
To retrieve the body parameter you can look for items in the req.Parameters collection where the Type is equal to ParameterType.RequestBody.
See code for the RestRequest class here.
Here is what the RestSharp docs on ParameterType.RequestBody has to say:
If this parameter is set, it’s value will be sent as the body of the
request. The name of the Parameter is ignored, and so are additional
RequestBody Parameters – only 1 is accepted.
RequestBody only works on POST or PUT Requests, as only they actually
send a body.
If you have GetOrPost parameters as well, they will overwrite the
RequestBody – RestSharp will not combine them but it will instead
throw the RequestBody parameter away.
For reading/updating the body parameter on-the-fly, you can try:
var body = req.Parameters.FirstOrDefault(p => p.Type == ParameterType.RequestBody);
if (body != null)
{
Console.WriteLine("CurrentBody={0}", body.Value);
body.Value = "NewBodyValue";
}
Or failing that, create a new copy of the RestRequest object with a different body.

How to send json using GET method to REST like API

I must implement method in my application which will send some data (which according to api documentation should be JSON) using GET method (it is weird...). How I can do this using c sharp in windows 8 (RestSharp lib is not working there). I don't have any experience in REST clients but I have already implement other features but there data was sended by POST or DELETE methods. I have tried "tranlate" json to get like this:
JSON:
{
a = "foo",
b = "bar
}
GET URL:
__SITE__?a=foo&b=bar
But server return null values (not error). I don't know how to deal with this thing :/
Thanks for help in advance :)
If you have api you have name of param that you should send. Just convert the data into json and sind is as this param.
If you have to send json why you`re sending param a and b as 2 diffrent strings ?
remember that a GET method can be invoked by HttpClient. Just invoke the URL
Finally it turned out (in my case) that API also accepts providing data in that way: URL?a=foo&b=bar regardless of the fact that it should be json.
Long story short, I think this will be most illuminating .. it "fills in the blanks" with using HttpClient to fire JSON Formatted data at a REST API
How do you set the Content-Type header for an HttpClient request?

Categories

Resources