I'm trying to do a POST request with a http body using AFNetworking. I used this post's answer
NSString *urlString = [NSString stringWithFormat:@"my-url"];
NSString *parameterData = [NSString stringWithFormat:@"request_type=get_story_nids&story_type=%@&story_number=%@", section, count];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[parameterData dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"content-type"];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id result) {
completionBlock(nids,nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
completionBlock(nil, error);
}];
I put breakpoints in the completion blocks of op setCompletionBlockWithSuccess and it doesn't even hit it.
If it helps, doing
NSMutableData* result = [[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error] mutableCopy];
works and gives me the right data.
Any ideas?
add break point to NSURL *url = [NSURL URLWithString:urlString]; and check do you get proper url if you get url null then try following code
NSURL *url = [[NSURL alloc] initWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSLog(@"url = %@",url);
I think you missed to add your operation to a operation queue. You can find an example here: http://samwize.com/2013/03/02/queue-http-operations-with-afnetworking/
// Add the operation to a queue
// It will start once added
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operationQueue addOperation:op];
You can also add https://github.com/AFNetworking/AFHTTPRequestOperationLogger to log all operations AFNetworking is dealing with.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.