iOS:如何正确地将put请求发送到服务器

时间:2014-10-21 12:40:16

标签: ios iphone http afnetworking put

我有一个我想要实现的看跌请求。我遇到的问题是它没有将正确的原始主体发送到服务器表单/ post参数。我期待的是一个{"questions":[{"type":"control_head"}]}的粗体,而我得到questions[][type]=control_head任何提示或建议都会受到赞赏。

  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]];
    }
}];
}

3 个答案:

答案 0 :(得分:0)

here

复制

AFNetworking 2.0再次更新 - 见底部

对于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);
}];

对于AFNetworking 2.0(以及使用新的NSDictionary语法):

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);
}];

答案 1 :(得分:0)

你可以使用afnetworking框架,它会帮助你。

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

答案 2 :(得分: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); 
                }];
相关问题