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

Posted

技术标签:

【中文标题】iOS:如何正确发送 put 请求到服务器【英文标题】:iOS : How to correctly send put request to server 【发布时间】:2014-10-21 12:40:15 【问题描述】:

我有一个我想要实现的 put 请求。我遇到的问题是它没有将正确的原始正文发送到服务器表单/发布参数。我期待的是"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]];
    
];

【问题讨论】:

你是 NSJSONSerialization 数据已经是 JSON 【参考方案1】:

复制自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);
];

【讨论】:

【参考方案2】:

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

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

【讨论】:

【参考方案3】:
 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); 
                ];

【讨论】:

以上是关于iOS:如何正确发送 put 请求到服务器的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Guzzle 发送 PUT 请求?

从 iOS 9 / Swift 2.0 发送 HTTP POST 请求时,PHP 服务器没有收到正确的数据

nodejs问题将graphql上传文件发送到服务

iOS:在这种情况下 put 请求如何工作?

nginx proxy_pass基于请求方法是POST,PUT还是DELETE

[尝试发送其他语言的PUT请求时出现服务器错误