AFNetworking JSON request for WCF REST service

I am using AFNetworking for my REST service (WCF). Here is the code:

NSDictionary *userName = [NSDictionary dictionaryWithObject:@"user" forKey:@"UserNameOrEmail"]; NSDictionary *pass = [NSDictionary dictionaryWithObject:@"123" forKey:@"Password"]; NSArray *credentials = [NSArray arrayWithObjects:userName,pass,nil]; NSDictionary *params = [NSDictionary dictionaryWithObject:credentials forKey:@"request"]; AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL: [NSURL URLWithString:@"http://server"]]; [client postPath:@"/ProfileWebService.svc/login" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { NSString *text = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"Response: %@", text); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"%@", [error localizedDescription]); }]; 

but I get 400 HTTP errors.

Using HTML and AJAX looks like this:

  $(document).ready(function () { $("#login_call").click(function () { $.ajax({ type: 'POST', url: 'http://server/ProfileWebService.svc/login', contentType : 'application/json', dataType: 'json', data: JSON.stringify({request: {UserNameOrEmail: $('#login_username').val(), Password: $('#login_password').val()}}), success: function (data) { $('#login_result').val('Code: ' + data.LoginResult.Code + '\nFault String: ' + data.LoginResult.FaultString); } }); }); }); 

and it works great.

What is wrong with the parameters.

+4
source share
1 answer

More detailed research and searching for AFNetworking documents on Stackoverflow gave me the SOLUTION :

1. The parameters for the request should be as follows:

  NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: [NSDictionary dictionaryWithObjectsAndKeys: username.text, @"UserNameOrEmail", password.text, @"Password", nil], @"request", nil]; 

2.Create an AFHTTPClient

  AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL: [NSURL URLWithString:@"http://server"]]; 

3. When I send JSON as parameters, I have to add:

  client.parameterEncoding = AFJSONParameterEncoding; 

After that I got rid of the 404 error, but I could not do anything with the JSON response, I do not know why. But the solution:

4.Create a request:

  NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"/ProfileWebService.svc/login" parameters:params]; 

5. Creating an operation:

  AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSLog(@"JSON: %@", [JSON valueForKeyPath:@"LoginResult"]); NSLog(@"Code: %@", [[[JSON valueForKeyPath:@"LoginResult"] valueForKeyPath:@"Code"] stringValue]); NSLog(@"FaultString: %@", [[JSON valueForKeyPath:@"LoginResult"] valueForKeyPath:@"FaultString"]); } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog(@"error opening connection"); }]; 

6. Run the operation:

  [operation start]; 

Hope that AFNetworking newbies find this post helpful.

+3
source

Source: https://habr.com/ru/post/1444625/


All Articles