问题
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