Search code examples
wcfrestios5streamingdatacontract

How to decode Post Request in WCF coming from iPhone?


I asked this question couple of times, and I have yet to be given a good answer. May be I am doing this wrong. I would like to send one Http Post request containing some text parameters and an image. in iPhone:

NSString reqstr = "param1=val1&param2=val2&..."
NSData *strData = [str DataUsingEncoding:NSUTF8StringEncoding]; //parameters
NSData *imageData = [NSData NSJPEGRepresenation(myImage.jpg,1)]; //image
NSMutableData *body = ...;
body.appendData = strData;
body.appendData = imageData;
req.setHttpBody = body;

In WCF:

void postData(Stream strm)
{
  //strm contains "param1=val1&param2=val2.....image raw binary here..........."
}

[DataContract]
class Employee { string param1,string param2, Stream photo..}
  • Is this the correct way of sending such an object? Is this not common between iphone and WCF?
  • How do I parse the bytes in the stream to read params especially to form the image from the binary data. Note If it was image byitself, I know how to do it. How do I get it out of the stream with other parameters?

Solution

  • WCF requires opt-In serialization, so tagging a class with [DataContract] is not enough to serialize any of it's members, any member you want serialized must also be tagged with [DataMember]. Also, there's no reason for you to send the stream itself, you should be able to do something like this instead:

    [DataContract]
    class Employee
    {
        [DataMember]
        string param1;
    
        [DataMember]
        string param2;
    
        [DataMember]
        JPEG image;
    }
    
    //Within your service, the postData contract will accept an Employee object from the caller // and perform some logic on it in the service.
    
    void postData(Employee emp)
    {
        //Do something with emp
    }
    

    Please let me know if you have any questions or if this is more of the same advice you've already gotten.