0

I want to send JSON data in URL body from my IOS app to server.I searched many SO question but i can't find what i want. If anyone knows how to do with AFNetworking then please let me know.Thanks This is my code snipped which send parameters

NSMutableDictionary *parameters = [[NSMutableDictionary alloc]init];
[parameters setValue:@"PY" forKey:@"AppSecret"];
[parameters setValue:@"IOS" forKey:@"login_provider"];
[parameters setValue:_txtemail.text forKey:@"email"];
[parameters setValue:_txtpassword.text forKey:@"password"];
[parameters setValue:@"1" forKey:@"ios_device_id"];


AFHTTPSessionManager *managertwo = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
managertwo.requestSerializer = [AFJSONRequestSerializer serializer];
[managertwo.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

[managertwo POST:[NSString stringWithFormat:@"http://capemedics.co.za/Api/user_register/valid_user"] parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSLog(@"success! %@",responseObject);
    NSLog(@"%@",parameters);

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"error: %@", error);
}];

And i am getting error in it. I want to pass JSON data like this in URL Body

{"login_provider": "IOS","email": "%@","password": ”%@“,”ios_device_id": "1"}
viratpuar
  • 524
  • 1
  • 5
  • 22
  • What did you tried? Can you post some code of what you tried? What is the error? – DrakaSAN Feb 18 '16 at 10:32
  • 1
    You can check this http://stackoverflow.com/questions/21487184/posting-json-data-using-afnetworking-2-0 – Reshmi Majumder Feb 18 '16 at 10:35
  • @reshmi this code only send parameters not JSON in Body – viratpuar Feb 18 '16 at 10:37
  • 1
    @viratpuar i will suggest you use AFNetworking but you have to use one common class for call WS url using NSObject. it is very easy and optimized code. check here http://stackoverflow.com/questions/33767908/volunteermatch-api-objective-c/33886449#33886449 – Vvk Feb 19 '16 at 04:50
  • but i want to to pass URL body as well.how to pass url body using AFNetworking? – viratpuar Feb 19 '16 at 05:05
  • @viratpuar In my test and reading the AFNetworking code, if you use AFJSONRequestSerializer, the parameters will be set as HTTP body. – YoonHo May 15 '19 at 02:00

2 Answers2

2

Try This

NSString *strData = [[NSString alloc] initWithFormat:@"{\"login_provider\": \"IOS\",\"email\": \"%@\",\"password\": \"%@\",\"ios_device_id\": \"1\"}",self.txtEmail.text,self.txtPassword.text];

                NSString *strURL = @"http://capemedics.co.za/Api/user_register/valid_user"];

                NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strURL] cachePolicy:NSURLRequestReloadIgnoringCacheData  timeoutInterval:10];

                [request setHTTPMethod:@"POST"];
                [request setValue: @"YOUR_KEY" forHTTPHeaderField: @"AppSecret"];
                [request setValue: @"application/json" forHTTPHeaderField:@"Content-Type"];
                [request setHTTPBody: [strData dataUsingEncoding:NSUTF8StringEncoding]];

                AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
                op.responseSerializer = [AFJSONResponseSerializer serializer];
                [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
                 {
                     if (responseObject)
                     {
                      }
                  }
                 } failure:^(AFHTTPRequestOperation *operation, NSError *error){
                 }];

                [op start];
Kamlesh Shingarakhiya
  • 2,757
  • 2
  • 16
  • 34
0

It worked before not sure

  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL
          cachePolicy:NSURLRequestReloadIgnoringCacheData  timeoutInterval:10];

 [request setHTTPMethod:@"POST"];

 [request setValue: @"application/json" forHTTPHeaderField:@"Content-Type"];
 [request setHTTPBody: [body dataUsingEncoding:NSUTF8StringEncoding]]; /// body is your son string

  AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
  op.responseSerializer = [AFJSONResponseSerializer serializer];
  [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

NSLog(@"JSON responseObject: %@ ",responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", [error localizedDescription]);

}];
[op start];
Reshmi Majumder
  • 961
  • 4
  • 15