Post array of object to WCF Rest service using JSON? - iphone

Need to send a complex object having nested IList from Iphone in JSON format to WCF REST service. The Complex Object is defined as following:
public class BatchData
{
long BatchID;
List<Account> Accounts;
List<Contacts> Contacts;
}
Please let me know the Client side (IPhone) syntax to create the required JSON request and also the server side POST method implementation to handle such scenario ?
Thanks in advance.

For the server side: define an operation contract which takes an array of that type. Search for examples on WCF Rest services (you'll need to use the [WebInvoke] attribute to define your operation) and you'll find how to do it.
For the client side: you can send the request using the NSURLRequest class. And to create the appropriate JSON, you can look at the NSJSONSerialization class, which will help you to convert between arrays (NSArray) and dictionaries (NSDictionary) and the JSON you need to send to the service.

Related

Handle Links in Restful Webservice Client

I'm implementing a client to a restful webservice, that returns results in a linked-list kind of way, where each element, besides the actual contents has a URI pointing to the next part of the result.
#XmlRootElement()
public class PagedResult<T> {
private List<T> values;
private URI next;
}
Using jax-rs(jersey) with jax-b there is plenty of documentation on how to create these links on the server side, but I'm not sure whats the preferred way to handle this on the client. I could obviously parse the URI and do the request myself, but that seems wrong.
Is there a simple way to get a getNext() method (returning PagedResult, not URI)?

Combining URL and POST variables in ServiceStack

I am trying to convert an existing wcf rest api to ServiceStack, and having issues right out of the gate:
[Route("foo/{userId}","POST")]
public class MyInputModel : IReturnVoid
{
public string userId { get; set; }
public SomeOtherObject properties { get; set; }
}
The intention here is that I would provide the userId in the url, and an instance of SomeOtherObject in the post body. The error I get is
<Message>Could not deserialize 'application/xml' request using MyInputModel'
Error: System.Runtime.Serialization.SerializationException:
Error in line 1 position 42. Expecting element 'MyInputModel'
from namespace 'blahblahblah'.. Encountered 'Element' with name
'SomeOtherObject', namespace 'http://blahblahblah'.
The only things I can think of are to wrap my xml in a MyInputModel to make the serializer happy. This is not really an option for backwards compatibility.
I could also modify SomeOtherObject to be the top level input model, and put a UserId property in there, but this also feels suboptimal since it is an object used throughout the api, and is really not tied to a user id. It is also already published independently, so it would be painful to make changes there.
Is there any way to indicate that the root element of the posted data will be a SomeOtherObject insted of a MyInputModel? In WebApi this would be with the [FromBody] attributes and whatnot. Does servicestack have anything similar?
The purpose of a DTO is to auto-generate the wire format which is why ServiceStack requires the Request DTO to match the shape of the incoming request. Part of what makes ServiceStack so productive is that it's a code-first web service framework which encourages starting from C# and projecting out, i.e. your clients should bind to your web service outputs and not the other way round of mapping code-first models to existing schema inputs.
Having said that, the Serialization / Deserialization wiki page lists the different ways to override ServiceStack's default request binding with your own.
Access HTTP Request variables in any Service or Filter
Not everything needs to be mapped to a DTO as any HTTP Variable can still be accessed from the IHttpRequest available from any service or filter, i.e:
base.Request.QueryString
base.Request.FormData
base.Request.Headers[name]
base.Request.PathInfo
base.Request.AbsoluteUri

How to sends multiple object over JSON from sitebricks client to sitebricks REST Service?

Is it possible to sends two or more object simultaneously from sitebricks client to sitebricks service ?
For example, I have a Person Object and a JobApplication Object. Can I do something like this:
public class ApplicationStorage {
#Post
public void post(Person person, JobApplication job) {
// ...
}
}
WebClient client = web.clientOf("service url") ..... // here how to send the two objects ?
If sending two objects is a must, then create another object that is composed of the two objects that you wish to send over HTTP. Your JSON parser (ex: Jackson) will be able to serialize each of the first two objects into JSON, and set them as JSON attributes to your new object.
You can send a List too, but this is not ideal as you have to mess about with Java Generics. The wrapper object is the best way.

JAX-RS generic response and interface proxy

is there any way how to return generic describing entity type with the JAX-RS Response? Something like REST-Easy ClientReponse but JAX-RS standard and not implementation-specific class.
The thing is I want to call my REST service via its shared interface (created by some proxy provider) and returning only object does not allow add information I need. E.g. for creating resource via POST, I would like to return also URL to newly created resource and so on. Returing simple Response does not show what type of entity is stored within such response.
Response<MyObject> getMyObject(#PathParam("id" Integer id)
So far it seems that I will have to return simple Response and then create adapter which will simply call Response.getEntity(.class)
There is probably no such option...
GenericEntity allows you to return a generic. The actual type is held at runtime by GenericEntity, allowing the object to be serialized.
Here's a contrived example of how it can be used.
GenericEntity entity = new GenericEntity<Employee>(new Employee());
return Response.ok(entity).build();

asp.net MVC 2.0 REST service with FormCollection

Let's say the Action below is exposed via REST service and is called from a different appliation how would it handle the posted data/object?
Should I use Create(FormCollection collection) here?
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Member member)
{
....
}
I'd suggest using a model, but one in which all of the parameters are nullable and use the RequiredAttribute for actual, required parameters. This would allow your method to accept invalid requests -- with missing or extra data -- yet have valid data bound to the model. For invalid data, you can supply error returns instead of presuming default values for non-nullable properties. Using the model binding validation architecture provides a convenient way to make sure that the request is legal. It would be up to you how you want to handle "extra" data supplied by the request -- I'd say ignore it.