Handling POST and PUT methods with Lullaby
UPDATE: See Change to RestClassAttribute and RestMethodAttribute.
When a POST or PUT method is sent to Lullaby, the request stream is deserialized to the type of the REST class that is currently being invoked and the REST method is invoked with the deserialized object. The REST method for a POST or PUT method needs to have one argument that is of the type of the enclosing REST class. Below is an example of a XML request stream and a simple Lullaby REST class and REST POST method.
<Address>
<AddressLine1>123 My Street</AddressLine1>
<AddressLine2>Suite 2</AddressLine2>
<City>Blahville</City>
<State>CA</State>
<Zip>90023</Zip>
</Address>
When the above request stream is deserialized to the below type of Address, the XML nodes will match up to their equivelent properties.
[RestClass]
public class Address
{
public int AddressID { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
[RestClass(HttpMethods.Method.POST)]
public Address CreateAddress(Address address)
{
//...
}
}
The address parameter in the CreateAddress method will be the result of the deserialized request stream.
For POST and PUT methods, a Content-Type header on the request must be specified. Lullaby.Serialization.SerializationTable handles routing of MIME types to serializers. By default, the following MIME types are mapped:
- application/javascript
- application/json
- application/json+javascript
- application/x-www-form-urlencoded
- application/xml
- text/javascript
- text/xml
Using the serialization table, you can change how MIME types are mapped to serializers and add and remove MIME type mappings. For example, if you wanted to add a MIME type of application/blah and map it to a serializer of BlahSerializer, you would do the following (BlahSerializer must implement Lullaby.Serialization.ISerializer):
SerializationTable.Deserializers.Add("application/blah", (valueToSerializeToObject, type) => new BlahSerializer.Deserialize(valueToSerializeToObject, type));
SerializationTable.Serializers.Add("application/blah", (objectToSerialize) => new BlahSerializer.Serialize(objectToSerialize));
Similar Posts
- Introducing Lullaby
- Change to RestClassAttribute and RestMethodAttribute
- Lullaby 1.0 Beta 3 released




