2

I use this method:

NSDictionary *params = @{@"email" : email,
                         @"password" : pass
                         };
NSString* HOST_URL = @"http://server.com:8080";

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:HOST_URL]];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
                                                        path:@"/login"
                                                  parameters:params];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
    NSDictionary *response = (NSDictionary*)[NSJSONSerialization JSONObjectWithData:operation.responseData options:kNilOptions error:nil];

    //HERE YOU HAVE A RESPONSE (your server answer)
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error.localizedDescription);
}];

[operation start];

And I have response:

{"Result":"fail","Message":"unexpected end of JSON input","Data":null}

My requirements is to send login and password via POST in variable "data", but how can I sent this variable on my server if I send request via text in dictionary here?

Rob
  • 415,655
  • 72
  • 787
  • 1,044
user3196922
  • 115
  • 2
  • 9
  • What server are you uploading to? If you need to send a POST in the way that a normal web form does, take a look at http://stackoverflow.com/questions/3508338/what-is-the-boundary-in-multipart-form-data – benathon Jan 16 '14 at 01:10
  • If you've concluded you want to use AFNetworking 2.0, as [your other question would suggest](http://stackoverflow.com/questions/21152847/how-to-post-data-using-afnetworking-2-0), then I'd suggest you accept user3200708's answer below (so he gets the "reputation" credit for helping you and you'll close this question). You really shouldn't have so many (four or five by my count) duplicative questions open at the same time. – Rob Jan 16 '14 at 04:40
  • I need send data in variable data, ye sorry for this – user3196922 Jan 16 '14 at 04:42
  • Thanks for cleaning up you old questions! I'll address your "variable data" question at [your other question](http://stackoverflow.com/questions/21152847/how-to-post-data-using-afnetworking-2-0). No point in having this discussion in the comments to multiple questions! – Rob Jan 16 '14 at 04:53

2 Answers2

2
NSDictionary *params = @{@"email" : email,
                     @"password" : pass
                     };
NSString* HOST_URL = @"http://server.com:8080";

AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager POST:HOST_URL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject){

// Enter what happens here if successsful.

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

// Enter what happens here if failure happens


}
0

I got this:

NSString *URLString = @"http://server.com:8080/login";
//    login = @"user1@1q2w3e"; // user1@1q2w3e user3@frcity
//    password = @"1q2w3e";

NSString* data = @"{\"email\": \"user1@1q2w3e\" ,\"password\": \"1q2w3e\"}";

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
NSDictionary *params = @{@"data" : data};
[manager POST:URLString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
user3196922
  • 115
  • 2
  • 9
  • http://stackoverflow.com/questions/42573527/calling-web-services-with-the-help-of-afnetworking-in-objective-c/42573800#42573800 – Harshit Goel Mar 03 '17 at 09:17