Error Code : Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set."

I hit this error when I using afNetworking PUT method. Please help.

Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

Here is my code:-

 NSInteger intUserID = [[prefs stringForKey:@"user_id"] integerValue];
                NSInteger intProdID = [_strProdID integerValue];

                AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
                AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
                [serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
                [serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
                manager.requestSerializer = serializer;

                NSDictionary *params = @{@"In d":@(1),@"user_id":@(intUserID),@"product_id":@(intProdID)};

                [manager PUT:@"http://api.XXX.com/api/product/wishlist_add" parameters:params success:^(NSURLSessionTask *task, id responseObject) {


                }failure:^(NSURLSessionTask *operation, NSError *error) {
                    NSLog(@"Error Code : %@", error);
                }];

1 answer

  • answered 2018-05-16 06:11 Hitesh Surani

    It clearly say you have sending wrong json format. Means there is two possiblty.

    • Either you have sending wrong json.

    • Or your server unable to parse data from your request.

    I has already faced same issue. Please use below code, I am sure it will work for you.

                NSInteger intUserID = [[prefs stringForKey:@"user_id"] integerValue];
                NSInteger intProdID = [_strProdID integerValue];
                NSDictionary *params = @{@"In d":@(1),@"user_id":@(intUserID),@"product_id":@(intProdID)};
    
                NSError *error; 
                NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params 
                                                                   options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                                     error:&error];
    
                if (! jsonData) {
                    NSLog(@"Got an error: %@", error);
                } else {
                    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
                AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
                AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
                [serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
                [serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
                manager.requestSerializer = serializer;
    
                [manager PUT:@"http://api.XXX.com/api/product/wishlist_add" parameters:jsonString success:^(NSURLSessionTask *task, id responseObject) {
                        }failure:^(NSURLSessionTask *operation, NSError *error) {
                            NSLog(@"Error Code : %@", error);
                        }];
                }
    

    Find reference from here

    Update:

    It is working fine. Please see below attached screenshot.

    enter image description here

    Please ensure user_id and product_id value should not be nil