0
votes

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);
                }];
2
it clearly says that your JSON format is invalid.Mahendra

2 Answers

1
votes

It clearly says you have sent wrong JSON format. Means there is two possibility.

  • Either you have sent wrong json.

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

I have already faced the same issue. Please use the 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

0
votes

This error comes because of mainly two reasons.

  1. Server unable to parse data from your request

  2. Sending wrong request

First try to call webservice on postman. If your webservice calling successfully and you get response data then validate that json response using json validator. If json response is valid then check your request.

Most of times problem is with the request we sent to the serve.

Happy Coding.