Prevent Restkit from adding escape characters?

孤者浪人 提交于 2019-12-12 06:20:10

问题


I am trying to encode image using base64 encoding and pass it through JSON , to generate JSON request and call RESTful API I am using RestKit.

What I have seen in the log is RestKit adds escape characters to encoded image, this is preventing server end from decoding image effectively and fails.

I want to know whats the best option to stop RestKit from adding escape characters

below is the example

VpT\/X8WWDCpj1XBpJ1zPBDuwLHLnpCZgnmLX3EXaffi0p7NklAPgO7HZsmxzC\/XITc\/K4iwRSG

One can see slashes (\/) added to the string.

Here is the code that i m using for encoding string

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

Base64.m as follows

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

I am passing this encoded string to RestKit request as a string


回答1:


If i am not wrong you are stuck at send image to server via restkit. My Friend try with multipart.

In Restkit v0.20.3 have few changes. Here i add code snippet for multipart post via RkObjectManager instance.

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 will complete multipart operation. Try with given code. Surely it will works.




回答2:


In the iOS 7 and Mac OS 10.9 SDKs, Apple introduced new base64 methods on NSData that make it unnecessary to use a 3rd party base 64 decoding library.

But as per my experience I faced issue while using Apple provided base64 methods, and was not able to decode my base64 pdf (from an .net server).

So I gave a try to this library and it worked for me as every time. I think this will work for you also.




回答3:


As restkit uses AFNetworking and it seems to be adding escape characters to encoded image my server end couldn't handle that , I have confirmed that this is AFNetworking issue not Restkit itself and seems like they have resolved it in 2.0 but since RestKit is stuck with old version it can't be solved , finally I gave up and opted for ASIHTTP library and passed JSON fabricated manually ensuring that base64 encoded image doesn't get tampered. That resolved the issue.



来源:https://stackoverflow.com/questions/29318016/prevent-restkit-from-adding-escape-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!