I am trying to POST JSON data to my asp.net MVC server from an iPhone app. The method in my server looks like this:
[HttpPost]
public String MyMethod(Stream anUpload) { ... }
And here is the code to POST the JSON from my app:
NSString *theURL = [NSString stringWithFormat:@"http://192.168.1.103/MyServer/MyMethod"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:theURL]];
[theRequest setHTTPMethod:@"POST"];
// Serialize my data.
NSData *theData = [NSJSONSerialization dataWithJSONObject:theList options:kNilOptions error:nil];
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[theRequest setValue:[NSString stringWithFormat:@"%d", [theData length]] forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:theData];
However, when I send the request, I just get an error message from the server, the MyMethod() never gets called. I am assuming that there is an issue with ASP accepting the POST data. Any ideas?
EDIT: Answer below
I found the answer:
I just needed to change the server-side function to
public String MyMethod(List<string> aList) { ... }The ModelBinder was trying to deserialize the JSON for me, and it didn’t like my parameter being a Stream.