-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
  • 68,014
  • 12
  • 152
  • 178
Gurp321
  • 109
  • 2
  • 10

3 Answers3

0

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

Bista
  • 7,769
  • 3
  • 25
  • 53
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,164
  • 4
  • 31
  • 59
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,590
  • 1
  • 11
  • 24
  • 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