AFNetworking 2.5.4使用PATCH请求发送文件

时间:2015-05-29 08:09:33

标签: objective-c afnetworking-2

使用AFNetworking进行REST API与我的应用程序之间的通信我遇到了一个奇怪的行为,试图上传带有PATCH请求的图像。

我使用以下代码:

- (void) uploadImage: (UIImage *)image {

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{};
    AFHTTPRequestSerializer *requestSerializer = [manager requestSerializer];
    NSError *e = nil;

    NSMutableURLRequest *request = [requestSerializer multipartFormRequestWithMethod:@"PATCH" 
                                                                           URLString:requestString 
                                                                          parameters:parameters 
                                                        constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        [formData appendPartWithFileData:UIImagePNGRepresentation(image)
                                    name:@"image"
                                fileName:@"image.png"
                                mimeType:@"image/png"];
    } error:&e];


    [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog("OK");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog("FAILED");
    }];
}

将不会达到success而非failed操作块。

Wenn我将请求方法更改为POST一切正常,但是在这种情况下,Server只接受PATCH方法。

我做错了吗?

1 个答案:

答案 0 :(得分:1)

我终于为我的问题找到了解决方案:

- (void)uploadImage:(UIImage *)image
              withSuccess:(SomeSuccessBlock)success
                  failure:(SomeFailureBlock)failure {

NSString *requestString = "Some url";
NSError *e = nil;

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestSerializer *requestSerializer = [manager requestSerializer];

NSMutableURLRequest *request = [requestSerializer multipartFormRequestWithMethod:@"POST" URLString:requestString parameters:@{}
                                                       constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

                                                           [formData appendPartWithFileData:UIImagePNGRepresentation(image)
                                                                                       name:@"user[avatar]"
                                                                                   fileName:@"avatar.png"
                                                                                   mimeType:@"image/png"];

                                                       } error:&e];

if (e && failure) {
    failure(e);
    return;
}

[request setHTTPMethod:@"PATCH"];
[request setValue:@"PATCH" forHTTPHeaderField:@"X-HTTP-Method-Override"];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    if (success) {
        success();
    }

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    if (failure) {
        failure(error);
    }

}];

    [operation start];
}