lundi 29 juin 2015

Url Encoding of dictionary with AFNetworking in Objective-c


i have to send POST request in following format to the server:
Content Type: application/x-www-form-urlencoded
Form Key : data
Form Value :

[
 {
   "email" : "test@test.com",
   "password" : "test@test.com"     
 }
]

When i send request by this format in web rest client (Postman/Advance Rest Client),i got success in response.

So how can i send this type of response with AFNetworking?

My code for Objective-c is

NSDictionary *dictLogin = @{@"email":@"test@test.com",@"password":@"test@test.com"};
NSDictionary *dictReq = @{@"data":@[dictLogin]};
NSData *data = [NSJSONSerialization dataWithJSONObject:dictReq options:NSJSONWritingPrettyPrinted error:nil];
NSString *strReq = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

[manager POST:@"http://test.php" parameters:dictReq success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

When i pass dictReq or str as AFNetworking parameter i got response of missing parameter/failure response from server

If it's not possible or hard with AFNetworking , NSUrlConnection/Request will also work

Thanks


Aucun commentaire:

Enregistrer un commentaire