Url Encoding of dictionary with AFNetworking in Objective-c

南笙酒味 提交于 2020-01-16 18:19:32

问题


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


回答1:


Try it this way, I've used AFNetworking for many projects. And ever had to encode Parameters.

NSDictionary *dictLogin = @{@"email":@"test@test.com",@"password":@"test@test.com"};
NSDictionary *dictReq = @{@"data":@[dictLogin]};

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);

}];


来源:https://stackoverflow.com/questions/31123793/url-encoding-of-dictionary-with-afnetworking-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!