1

I'm using AFNetworking framework to send some data to the .Net backend. I'm using POST:parameters:success:failure: method.

The .net backend is reading data using the FromBody attribute

 [System.Web.Mvc.HttpPost]
        public HttpResponseMessage Post([FromBody] string objCourseData, string securityToken, string appVer, string deviceDesc, string deviceOSDesc)

but it receives it as null. I've tried with PHP and I can see my body data in the $_POST object (I can't see it in the $HTTP_RAW_POST_DATA though). What could be the problem?

The data I'm sending is JSON object.

Lukasz 'Severiaan' Grela
  • 6,078
  • 7
  • 43
  • 79

1 Answers1

2

I don't use AFNetworking, but using native code is simple:

NSString *urlString = @"http://www.urlpost.com";

//setting request
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url
                                                           cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                       timeoutInterval:5];
[request setHTTPMethod:@"POST"];

//setting post body with JSON data
NSMutableDictionary *postDictionary = [NSMutableDictionary dictionary];
//set the data of your json
[postDictionary setValue:@"some value" forKey:@"myKey"];
NSError *jsonError;

//transform NSMutableDictionary in JSON data
NSData *postData = [NSJSONSerialization dataWithJSONObject:postDictionary
                                                   options:0
                                                     error:&jsonError];

if (!jsonError) {
    //set POST body
    [request setHTTPBody:postData];
}

//Here is the code for the post:
[[[NSURLSession sharedSession] dataTaskWithRequest:request
                                 completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

    if (!error) {
        //No errors, post done
        NSLog(@"POST done!");
    } else { 
       //error handling here 
    }
}] resume];
Claudio
  • 1,987
  • 3
  • 29
  • 55
  • I've accepted this as it gave me clear access to details of the request, I'm sure similar can be achieved with `AFNetworking` but failed to find resolution. – Lukasz 'Severiaan' Grela Jul 16 '14 at 11:39
  • I haven't used AFNetworking, but after set the request, you just need to do the download as you have been doing – Claudio Jul 16 '14 at 16:42