如何使用AFNetworking为“PUT”请求设置数据?

我已经开始使用AFNetworking,当它做出简单的“GET”请求时,它运行良好。 但是现在我正在尝试做一个“POST” – 请求。 我使用下面的代码来执行“GET”请求。 查看AFHTTPClientputhPath时 ,没有办法设置要用于身体的数据。 我的猜测是有另一种解决方法。 我一直在看AFHTTPOperation作为解决这个问题的方法。 但是我没有得到这个工作。 问题是,我不知道如何使用它与基本authentication。

有人能给我提示如何做一个简单的“POST” – 与AFNetworking的请求?

AFHTTPClient* client = [AFHTTPClient clientWithBaseURL:ServerURL]; [client setAuthorizationHeaderWithUsername:self.username password:self.password]; NSString* resourcePath = [NSString stringWithFormat:@"/some/resource/%@", endPath]; [client getPath:resourcePath parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { // Success code omitted } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // Some error handling code omitted } ]; 

我没有find任何简单的方法来做到这一点。 但是我按照build议做了,创build了我自己的AFHTTPClient的子类。 在子类中,我实现了下面的方法。 这使得可以使用我自己的数据执行POST请求和PUT请求。

 - (void)postPath:(NSString *)path parameters:(NSDictionary *)parameters data:(NSData*)data success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; { NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters data:data]; AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure]; [self enqueueHTTPRequestOperation:operation]; } - (void)putPath:(NSString *)path parameters:(NSDictionary *)parameters data:(NSData*)data success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; { NSURLRequest *request = [self requestWithMethod:@"PUT" path:path parameters:parameters data:data]; AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure]; [self enqueueHTTPRequestOperation:operation]; } -(NSMutableURLRequest*)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters data:(NSData*)data; { NSMutableURLRequest* request = [super requestWithMethod:method path:path parameters:parameters]; [request setHTTPBody:data]; return request; } 

随着AFNetworking 2.0,我只是复制代码

 - (AFHTTPRequestOperation *)PUT:(NSString *)URLString parameters:(id)parameters success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; 

并添加一个

 [request setHTTPBody:data]; 

就这个:

 NSString* str = [bookDetailLink objectForKey:@"Body"]; NSData* data = [str dataUsingEncoding: NSUTF8StringEncoding]; NSMutableURLRequest *request = [self.manager.requestSerializer requestWithMethod:@"PUT" URLString:bookingDetailUrl parameters:nil error:nil]; [request setHTTPBody:data]; AFHTTPRequestOperation *operation = [self.manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *op, NSHTTPURLResponse *response) { NSLog(@"%@", response); } failure:^(AFHTTPRequestOperation *op, NSError *error) { NSLog(@"%@", error); }]; [self.manager.operationQueue addOperation:operation]; 

我正在使用AFNetworking将Skyscanner API集成到我们的iOS应用程序中。

使用AFNetworking 1.3.2,以下代码适用于我:

 NSData *imageData = UIImageJPEGRepresentation(thumb, 0.85F); AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"https://example.com/"]]; NSMutableURLRequest *request = [httpClient requestWithMethod:@"PUT" path:@"/foo" parameters:nil]; [request setHTTPBody:imageData]; [request setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"]; AFHTTPRequestOperation *operation = [httpClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *op, NSHTTPURLResponse *response) { NSLog(@"%@", response); } failure:^(AFHTTPRequestOperation *op, NSError *error) { NSLog(@"%@", error); }]; [operation start]; 

这导致一个正确的标题,内容长度和一般RESTfulness的PUT请求:-)