-1

How to send NSDate in JSON Format through AFNetworking in iOS ? Json request -

{
    "from_date" : "11/11/2014",
    "to_date" : "11/11/2017"
}

Code -

// Formats the date chosen with the date picker.
- (NSString *)formatDate:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:@"dd'/'MM'/'yyyy"];
    NSString *formattedDate = [dateFormatter stringFromDate:date];
    return formattedDate;
}
    - (void)updateToDateField:(id)sender
    {
        UIDatePicker *picker = (UIDatePicker*)toDateField.inputView;
        toDateField.text = [self formatDate:picker.date];
    }
    // Storing the to date in NSString
        dashboard.toDateString = toDateField.text;
Nirav D
  • 71,513
  • 12
  • 161
  • 183
Gurp321
  • 109
  • 2
  • 11

3 Answers3

0

Convert your NSDate in to String and send it, just like you send other parameters.

Bista
  • 7,869
  • 3
  • 27
  • 55
0

Try this

 NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
   [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
   NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];

now use dateString in you json

Anupam Mishra
  • 3,408
  • 5
  • 35
  • 63
0

I think you are forgot to serialize your request while calling API using AFNetworking Manage class.

Try to add below line in your code.

    AFHTTPRequestOperationManager *manager;
     manager = [AFHTTPRequestOperationManager manager];
     manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:strRequestUrl parameters:dictParams success:^(AFHTTPRequestOperation *operation, id responseObject)
        {

            NSString *msg;
            NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObject
                                                               options:NSJSONWritingPrettyPrinted
                                                                 error:nil];
            NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];


        } failure:^(AFHTTPRequestOperation *operation, NSError *error)
        {

            NSLog(@"request : %@",operation.request);
            NSLog(@"Error is %@",error);

        }];
Bhadresh Mulsaniya
  • 2,610
  • 1
  • 12
  • 25
  • I have added this. I think, there would be some format to send date string so that the .net server web api would accept it. – Gurp321 Aug 29 '16 at 10:59