0

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);
                }];
Test 87
  • 101
  • 2
  • 10

2 Answers2

1

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

Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65
  • Hi Hitesh, thanks for your prompt feedback. I hit this error after applied your code:- *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write' – Test 87 May 16 '18 at 07:24
  • Sorry for the inconvenience, But it working fine and covert as per requirement. You can alos check in above attached screenshot – Hitesh Surani May 16 '18 at 09:03
0

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.

Protocol
  • 1,696
  • 13
  • 30