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);
}];
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.
Please ensure user_id
and product_id
value should not be nil