2

I have a wcf service which accepts a parameter like this:

[DataContract]
public class Person
{
    [DataMember]
    public int ID { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Family { get; set; }
}

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, UriTemplate = "")]
int InsertPerson(Person person);

I am familiar how to consume a wcf service with string parameter from Objective-C but in this case I want to pass an instance of Person class as parameter. How can I do that?

 NSString *path = [[NSString alloc] initWithFormat:@"http://192.168.0.217/JSON/Service1.svc/InsertPerson"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:path]];

[request setHTTPMethod:@"POST"];  

[[NSURLConnection alloc] initWithRequest:request delegate:self];
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
ali
  • 1,023
  • 2
  • 14
  • 38

2 Answers2

1

(From memory only here!)

Try setting the body of the request thus:

{ person: { ID: 1, Name: "Joe", Family: "Bloggs" } }

And then remember to send the Content-Type header as application/json, as well as making sure you use the POST method.

I'm sorry I can't actually give you the actual code for doing this - I'm not an objective-c developer.

Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
1

Something like this code should work:

NSArray *propertyNames = [NSArray arrayWithObjects:@"ID", @"Name", @"Family", nil];
NSArray *propertyValues = [NSArray arrayWithObjects:@"123", @"joe", @"smith", nil];

NSDictionary *properties = [NSDictionary dictionaryWithObjects:propertyValues forKeys:propertyNames];
NSMutableDictionary* personObject = [NSMutableDictionary dictionary];
[personObject setObject:properties forKey:@"person"];

NSString *jsonString = [personObject JSONRepresentation];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.0.217/JSON/Service1.svc/InsertPerson"]];
[request setValue:jsonString forHTTPHeaderField:@"json"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:jsonData];

NSError *errorReturned = nil;
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
if (errorReturned) {
    //...handle the error
}
else {
    NSMutableString *retVal = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    //...do something with the returned value

    [retVal release];
}
[theResponse release];
Sixto Saez
  • 12,610
  • 5
  • 43
  • 51
  • i know this is old but, the JSONRepresentation line is giving me this error **'no visible interface for NSMutableDictionary declares the selector JSONRepresentation'** – gdubs Jan 03 '13 at 09:48
  • Please look at this [SO answer](http://stackoverflow.com/questions/7143863/jsonrepresentation-for-nsmutabledictionary?answertab=votes#tab-top) to see which are the proper libraries to include. – Sixto Saez Jan 03 '13 at 14:21
  • Include SBJson Library to use JSONRepresentation. You can get it from here https://github.com/stig/json-framework – Dilip Manek Jul 04 '13 at 07:54