Return raw string from REST service method - rest

I have a REST service method written in C#, defined as below:
[WebGet(UriTemplate = "/{par1}/{par2}/{par3}")]
public string ProcessGet(string par1, string par2, string par3)
{
return Execute(...);
}
It should return result as XML or JSON, based on one parameter (I generate the json and XML serialization)
How can I make this method to return the RAW string, just as I created it, without HTMLEncoding it?
Thank you

Return it as a Stream - that causes the "raw" mode to be used and WCF will not touch your response. You can find more information at http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx.

Related

NET Core 5 : How to pass string list as a parameter?

My app is: NET Core 5 Web service.
I need to pass to HTTP PUT method a csv file.
The csv is like string1, string2, string3.
As one way I see: upload the csv file and then parse it to corresponding class.
But what else way? How to pass the string list in RESTful API?
Assuming your string list is longer than an URL should be, you define a body:
public class Model
{
public List<string> StringList { get; set; }
}
Then in your controller, you chose a HTTP method that accepts a body (PUT/POST most likely):
[HttpPost]
public IActionResult YourMethod([FromBody] Model model)
{
// and here, you can access model.StringList
}
Please note that an XLS or even CSV file is not just a string list. You would be better served by uploading the file and using an actual parser package that can handle that file format.

AndroidAnnotations, REST and custom response

I'm using AndroidAnnotations in my project. This works fine so far...
But now I try to access a given Webservice with a custom response like this:
{
success: true,
total: 100,
items: [],
}
This response format is the same for every GET request. The items property holds the response objects I'm interested in and the items contains different object type(like user, product etc)
I'm using "MappingJackson2HttpMessageConverter"...
How can I access them?
Thanks,
Stefan
You have to create the response class, which can be deserialized from the JSON response:
public class CustomResponse {
boolean success;
int total;
List<Integer> items;
}
This is just an example, because it is ambigous from your JSON snippet, what exact types you are using.
Then you can use this response class in your REST client:
#Get("/getsomething")
public CustomResponse getSomething();
It is not necessary to use MappingJackson2HttpMessageConverter, you could use any converter which can convert your JSON response (like a GSON converter).

Spring MVC REST

I'm using Spring MVC and I have a controller mapped to a url lets call it example. I also have a method called show that allows me to view one of my examples based on an id.
#RequestMapping("/example")
#RequestMapping(value = "/{id}", produces = "text/html")
public String show(#PathVariable("id") String id, Model model) {
//Do some stuff and return a view
}
The problem is that the id is a URI and it has forward slashes. (e.g. test/case/version/sample might be an id so the resulting url is example/test/case/version/sample) so as a result my application gives me an error "Requested resource not found". I can't easily change the format of these ids. It's a list given to me that I have to work with. Is there a way around this? Thanks in Advance.
You can try using Regular expressions on the #PathVariable.
Like this from the Spring Docs:
#RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\\d\\.\\d\\.\\d}{extension:\\.[a-z]+}")
public void handle(#PathVariable String version, #PathVariable String extension) {
// ...
}
}
You'll just have to think on a regular expression that matches the "example/test/case/version/sample" that is your expression.
See the title: "URI Template Patterns with Regular Expressions"
on this page: http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html for more information

Error Generating XML Document from object

I am wishing to serialize a complex object for returning from a web service request. Here are my assumptions. I need to have the serialized (deflated) object in an XML document (as opposed to a string) before returning to the calling client. I "believe" I am deserializing just fine it is just a matter of getting it loaded into the XMLDocument. However I could be wrong and the deserialization may be wrong therefore the XmlDocument blows up. Here is the code:
My Complex Object:
namespace ABCTest
{
[XmlRoot("TapRoot")]
public class UserDetails
{
[XmlElement]
public String AccountName { get; set; }
}
}
My serialization code:
FYi: UsrDtls == List<UserDetails>
XmlSerializer Obj2XML = new XmlSerializer(UsrDtls.GetType());
Stream strWriter = Stream.Null;
XmlWriter XWriter = new XmlTextWriter(strWriter, Encoding.Unicode);
XmlDocument XDoc = new XmlDocument();
Obj2XML.Serialize(XWriter, lst_Exercises);
string abc = Obj2XML.ToString(); //debugging line to attempt to browse the obj2xml object
XDoc.LoadXml(abc);
return XDoc;
I have no idea where you learned about web services in .NET. Just return the object. The web service infrastructure will take care of it.
You don't say whether you're using WCF services or the legacy ASMX services. The ASMX services should not be used for new development.
If you still have trouble when you "just return it", then please post the details of any exceptions you receive.

Are list parameters supported in OpenRasta? how they should be sent?

If I have a resource handler method receiving an object list as parameter (int list, string list or any object type list), i.e.:
public class TasksCollecionHandler
{
public TaskCollection GetTasksByEngineer(List<int> engineerIds)
{
....
}
public TaskCollecion GetTasksByEngineer(List<Engineer> engineers)
{
....
}
}
Is OpenRasta supporting this kind of arguments?
How the HTTP request including values for kind of arguments should look like?
Depends on the codec. Using json or xml data contracts it'd be whatever format the datacontract mapping follows.
If you use forms and html, the format would be
engineers:0.FirstName=john&engineers:0.LastName=doe&engineers:1.FirstName=other