防止 Restkit 添加转义字符?

Prevent Restkit from adding escape characters?

我正在尝试使用 base64 编码对图像进行编码并通过 JSON 传递它,以生成 JSON 请求并调用 RESTful API 我正在使用 RestKit。

我在日志中看到的是 RestKit 向编码图像添加了转义字符,这阻止了服务器端有效解码图像并失败。

我想知道阻止 RestKit 添加转义字符的最佳选择是什么

下面是例子

VpT\/X8WWDCpj1XBpJ1zPBDuwLHLnpCZgnmLX3EXaffi0p7NklAPgO7HZsmxzC\/XITc\/K4iwRSG

可以看到字符串中添加了斜杠 (\/)。

这是我用来编码字符串的代码

NSData *originalPhoto = UIImagePNGRepresentation([UIImage imageNamed:@"Time_Icon.png"]);
NSString *base64PhotoString = [Base64 encode:originalPhoto];

Base64.m如下

+ (NSString*) encode:(const uint8_t*) input length:(NSInteger) length {
    NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t* output = (uint8_t*)data.mutableBytes;

    for (NSInteger i = 0; i < length; i += 3) {
        NSInteger value = 0;
        for (NSInteger j = i; j < (i + 3); j++) {
            value <<= 8;

            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }

        NSInteger index = (i / 3) * 4;
        output[index + 0] =                    encodingTable[(value >> 18) & 0x3F];
        output[index + 1] =                    encodingTable[(value >> 12) & 0x3F];
        output[index + 2] = (i + 1) < length ? encodingTable[(value >> 6)  & 0x3F] : '=';
        output[index + 3] = (i + 2) < length ? encodingTable[(value >> 0)  & 0x3F] : '=';
    }

    return [[[NSString alloc] initWithData:data
                                  encoding:NSASCIIStringEncoding] autorelease];
}


+ (NSString*) encode:(NSData*) rawBytes {
    return [self encode:(const uint8_t*) rawBytes.bytes length:rawBytes.length];
}

我将这个编码的字符串作为字符串传递给 RestKit 请求

如果我没记错的话,你被困在通过 restkit 将图像发送到服务器。我的朋友尝试使用 multipart。

在 Restkit v0.20.3 中有一些变化。在这里,我通过 RkObjectManager 实例为 multipart post 添加了代码片段。

NSString *strTextToPost = self.txtViewForPost.text;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    mediaType ? [params setObject:mediaType forKey:@"MEDIA_TYPE"] : @"";
    strTextToPost ? [params setObject:strTextToPost forKey:@"POST_TEXT"] : @"" ;

    NSMutableURLRequest *request = [[AppDelegate appDelegate].rkomForPost multipartFormRequestWithObject:nil method:RKRequestMethodPOST path:strPath parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:contentData
                                    name:@"FILE"
                                fileName:fileName
                                mimeType:fileType];
    }];

    RKObjectRequestOperation *operation = [[AppDelegate appDelegate].rkomForPost objectRequestOperationWithRequest:request success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        [RSActivityIndicator hideIndicator];
        NSLog(@"%@",operation.HTTPRequestOperation.responseString);
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        [RSActivityIndicator hideIndicator];
        NSLog(@"%@",operation.HTTPRequestOperation.responseString);
    }];
    [[AppDelegate appDelegate].rkomForPost enqueueObjectRequestOperation:operation];

enqueueObjectRequestOperation 将完成多部分操作。尝试给定的代码。肯定有用。

在 iOS 7 和 Mac OS 10.9 SDK 中,Apple 在 NSData 上引入了新的 base64 方法,因此无需使用第三方 base 64 解码库。

但根据我的经验,我在使用 Apple 提供的 base64 方法时遇到了问题,并且无法解码我的 base64 pdf(来自 .net 服务器)。

所以我尝试了 this 库,它每次都对我有用。我想这对你也有用。

由于 restkit 使用 AFNetworking 并且它似乎正在向编码图像添加转义字符我的服务器端无法处理,我已经确认这是 AFNetworking 问题而不是 Restkit 本身并且他们似乎已经在 2.0 中解决了它但是由于 RestKit 卡在旧版本上无法解决,最后我放弃并选择了 ASIHTTP 库并通过 JSON 手动制作以确保 base64 编码图像不会被篡改。那解决了这个问题。