0

I have a put request that I am trying to achieve. The problem I am having is it isn't sending the correct raw body over to the server form/post parameter. What I am expecting is a raw body that is {"questions":[{"type":"control_head"}]}, instead I am getting questions[][type]=control_head any tips or suggestions are appreciated.

  NSString *jsonString = @"{\"questions\":[{\"type\":\"control_head\"}]}";
 [self createForms:jsonString];

 - (void) createForms : (NSString *) form
 {
 [self executePutRequest:@"user/forms" params:form];
  }

- (void) executePutRequest : (NSString *) url params : (NSString *) params
{
NSString *urlStr = [NSString stringWithFormat:@"http://requestb.in/13oujhot"];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

NSData *data = [params dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

NSMutableDictionary *userinfo = [[NSMutableDictionary alloc] init];

[manager PUT:urlStr parameters:json success:^(AFHTTPRequestOperation *operation, id responseObject) {
    [operation setUserInfo:userinfo];
    SBJsonParser *jsonparser = [SBJsonParser new];
    id result = [jsonparser objectWithString:[operation responseString]];
    if ( self.delegate != nil && [self.delegate respondsToSelector:finishSelector] ) {
        [self.delegate performSelector:finishSelector withObject:result];
    }

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    [operation setUserInfo:userinfo];
    if ( self.delegate != nil && [self.delegate respondsToSelector:failSelector] ) {
        [self.delegate performSelector:failSelector withObject:[operation error]];
    }
}];
}
James Well
  • 17
  • 3

3 Answers3

0

Copied From here

Updated again for AFNetworking 2.0 - see bottom

For AFNetworking 1.0:

NSURL *url = [NSURL URLWithString:@"https://mysite.com/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                        height, @"user[height]",
                        weight, @"user[weight]",
                        nil];
[httpClient postPath:@"/myobject" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"Request Successful, response '%@'", responseStr);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
}];

For AFNetworking 2.0 (and also using the new NSDictionary syntax):

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"user[height]": height,
                         @"user[weight]": weight};
[manager POST:@"https://mysite.com/myobject" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
Community
  • 1
  • 1
Danial Hussain
  • 2,488
  • 18
  • 38
0

you can use afnetworking framework, it will help you.

http://www.raywenderlich.com/59255/afnetworking-2-0-tutorial

Dheeraj Kumar
  • 490
  • 3
  • 12
0
 NSString *str_URL = [NSString stringWithFormat:@"YOUR URL"]];


 NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
                                                    Your Value,KEY_Name,
                                                    nil];

 AFHTTPRequestOperationManager *Manager = [AFHTTPRequestOperationManager manager];

 [Manager PUT:str_URL parameters:parameters
      success: ^(AFHTTPRequestOperation *operation, id responseObject)
                 {
                             NSLog(@"%@", responseObject);
                 }
      failure:^(AFHTTPRequestOperation *operation, NSError *error)
                {
                             NSLog(@"%@", error.description); 
                }];