1
votes

I am trying to upload a image using AFHTTPSessionManager through REST API call.

following are my code

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager.requestSerializer setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];
    NSData *imageData = UIImageJPEGRepresentation(image, 0.5);

    [manager POST:@"http://localhost:8888/uploadImage/100" parameters:nil constructingBodyWithBlock:^(id formData) {
        [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"success:%@", responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"Error:%@", error);
    }];

Response is going to field block and getting following error

Error:Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x9cbc690 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set., NSUnderlyingError=0x9cbaf00 "Request failed: length required (411)"}

Any one have idea on this issue ?

Thanks

2
To where are you uploading the image? Are you sure the server is expecting a binary and not a text? I mean, may be you should upload as base64 - David Bemerguy
Some code thinks that you are trying to process JSON data and that fails. Since you don't seem to intend this, it's obvious that this would fail. Debug your code and try to find out why someone tries to process JSON data. Maybe something leftover from a previous request? - gnasher729

2 Answers

0
votes
+(void)uploadImageData : (NSString *)url parameters:(NSDictionary *)dparameters imageData:(NSData *)dimageData success: (void (^) (NSDictionary *responseStr))success failure: (void (^) (NSError *error))failure
{
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:url]];

    AFHTTPRequestOperation *op = [manager POST:url parameters:dparameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        //do not put image inside parameters dictionary as I did, but append it!
        [formData appendPartWithFileData:dimageData name:@"image" fileName:@"photo11.jpg" mimeType:@"image/jpeg"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //success

        success(responseObject);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // failure
        failure(error);
    }];
    op.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [op start];

}
0
votes

The error

JSON text did not start with array or object and option to allow fragments not set

Indicates that the server sent JSON with a fragment which isn't supported by the iOS JSON parser by default. You can enable it by setting the AllowFragments option for readingOptions on your AFJSONResponseSerializer.

Try to add (Swift)

manager.responseSerializer = AFJSONResponseSerializer(readingOptions: NSJSONReadingOptions.AllowFragments)

This solves for me.