0
votes

I'm pretty new to Xcode, and I'm trying to make a POST request. I searched on Google and found this AFNetworking library. I've read some documentation but they all say different things. So now I use their own example on a POST request and my code looks like this:

- (void)viewDidLoad {
    [super viewDidLoad];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{@"fName": @"firstname", @"lName": @"lastname"};
    [manager POST:@"http://00000.1111.ovh/api/Account/register" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

And then I get this error:

Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo={com.alamofire.serialization.response.error.response= { URL: http://00000.1111.ovh/api/Account/register } { status code: 400, headers { "Content-Length" = 159; "Content-Type" = "application/json; charset=utf-8"; Date = "Sun, 15 Nov 2015 12:11:57 GMT"; Server = "Microsoft-IIS/8.5"; "X-Powered-By" = "ASP.NET"; } }, NSErrorFailingURLKey=http://00000.1111.ovh/api/Account/register, com.alamofire.serialization.response.error.data=<7b226d65 73736167 65223a22 54686520 72657175 65737420 69732069 6e76616c 69642e22 2c226d6f 64656c53 74617465 223a7b22 6c6f6769 6e2e7573 65726e61 6d65223a 5b225468 65205573 6572206e 616d6520 6669656c 64206973 20726571 75697265 642e225d 2c226c6f 67696e2e 70617373 776f7264 223a5b22 54686520 50617373 776f7264 20666965 6c642069 73207265 71756972 65642e22 5d7d7d>, NSLocalizedDescription=Request failed: bad request (400)}

Hope someone can help me!

2
Do yourself a favour and dont use this Framework. use NSURLSession instead.Thallius
Claus, I'd hesitate to advise against using this framework. For new users, properly encoding requests is very complicated (e.g. 99% of the examples I see here online fail to percent-escape requests properly). Sure, if you don't like the fact that AFHTTPRequestOperationManager is NSURLConnection based (which is now deprecated), then use the very similar AFHTTPSessionManager (which is NSURLSession based).Rob
@Claus: though AFNetworking might be an overkill in some (especially simple) cases you are giving an "i don't get your question but i am willing to answer it anyway" advice here...Rok Jarc

2 Answers

2
votes

You can better diagnose these issues if you translate the server's response into something you can parse/examine:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"fName": @"firstname", @"lName": @"lastname"};
[manager POST:@"http://00000.1111.ovh/api/Account/register" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON = %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error = %@", error);
    if (operation.responseObject) {
        NSLog(@"responseObject = %@", operation.responseObject);
    } else {
        NSLog(@"responseString = %@", operation.responseString);
    }
}];

If you did that, you'd see that it responded with:

responseObject = {
    message = "The request is invalid.";
    modelState =     {
        "login.password" =         (
            "The Password field is required."
        );
        "login.username" =         (
            "The User name field is required."
        );
    };
}

Apparently, the request was missing the necessary authentication details.

1
votes

Your request is sent correctly, but the server responded with HTTP code 400. Check the server's API documentation on how the request should look like.