1

I am getting this error while posting json to url :

 Invalid top-level type in JSON write'

I am even sending perfect JSON using afnetworking post request code But it keeps giving me error.

My code :

NSString *  serviceType = @"xyz";
NSString *  remarks =@"project deadline is near ";
NSString *  emailId = @"xyx.live.com";


NSDictionary *params =
@ {
    @"serviceType" :serviceType,
    @"remarks" :remarks,
    @"emailId" :emailId

};

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params
                                                   options:0 
                                                     error:&error];


NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];



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



[manager2 POST:@"http://xyx.com" parameters:jsonString success:^(AFHTTPRequestOperation *operation, id responseObject) {
     NSLog(@"JSON_post_fzzlogin: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {



    //here is place for code executed in success case
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error while sending POST"
                                                        message:@"Sorry, try again."
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];

    NSLog(@"Error: %@", error);

}];

Please help me out this regard .thanks in advance

2 Answers 2

1

You are trying to encode request twice because you set request serializer for AFHTTPRequestOperationManager and provide parameters as encoded JSON string. So you need to provide raw parameters if you want to use request serializer or don't use it and encode to JSON manually.

Sign up to request clarification or add additional context in comments.

Comments

1

This is how you post JSON or rather NSDictionary by AFNetworking

NSDictionary *dict = @{@"Key":@"Value"};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:[Util urlAppend:kListOfArticleCatagory] parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {

NSDictionary *responseObj = (NSDictionary *)responseObject;//casting to NSDictionary from id
NSLog(@"%@",responseObj);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Something went wrong :%@",operation.responseString);

}];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
}

You dont post JSON through AFNetworking, rather the NSDictionary as the parameters, it will get converted to JSON as you post it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.