0

sample post request
I am able to make web services through AFHttpClient but since library new version doesn't allowed it. Please help me in creating post body. As i can pass dictionary with parameters but i want to send it as post string.

A sample code will be very help full because i have already searched on internet and cant able to make request. // this is the code but its in AFhttpClient

   -(void)reportBullyWithAfnetworking{


        NSMutableURLRequest *request = [[NSMutableURLRequest alloc ] initWithURL:[NSURL URLWithString:BullyAlertUrlReport] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:180.0];



        NSString* getLoginData;


        NSLog(@"getLoginData %@", getLoginData);
        NSError *error;

        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[AppDelegate getAppDelegate].mutableCopy options:NSJSONWritingPrettyPrinted error:&error];
        getLoginData = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

        int contentLength = [getLoginData lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
        NSString* contentLengthStr = [[NSString alloc] initWithFormat:@"%d", contentLength];


        [request setHTTPMethod:@"POST"];
        [request setValue:contentLengthStr forHTTPHeaderField:@"Content-Length"];
        [request setHTTPBody:[NSData dataWithBytes:[getLoginData UTF8String] length:contentLength]];
        [request setValue:@"application/x-www-form-urlencoded"  forHTTPHeaderField:@"Content-Type"];


        AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:BullyAlertUrlReport]];
        [httpClient setParameterEncoding:AFFormURLParameterEncoding];



        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            // Print the response body in text

            NSString* responseString=[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            NSLog(@"Response: %@",responseString);

            //[SVProgressHUD showSuccessWithStatus:@"Great Success!"];

            SBJSON *jsonParser=[[SBJSON alloc]init];
            NSMutableDictionary *response=(NSMutableDictionary *)[jsonParser objectWithString:operation.responseString error:nil];

            NSLog(@"resposeCode = %@",response);

        }
        failure:^(AFHTTPRequestOperation *operation, NSError *error){
                                             NSLog(@"error code %d",[operation.response statusCode]);
                                         }];

        [operation start];
    }
DreamWatcher
  • 399
  • 1
  • 3
  • 11

0 Answers0