I am trying to post to a URL using AFNetworking and no matter what I do I keep getting the error:
Error Code: -1011 - Expected status code in (200-299), got 404
My code is as follows:
NSString *baseurl = @"http://mysiteurl";
NSString *path = @"/user/register/";
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:baseurl]];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
//[client setAuthorizationHeaderWithUsername:@"myusername" password:@"mypassword"];
[client postPath:path parameters:[NSDictionary dictionaryWithObjectsAndKeys:_userName,@"user", _email, @"email",_password,@"password", nil] success:^(AFHTTPRequestOperation *operation, id JSON) {
//NSLog(@"sjson: %@", [JSON valueForKeyPath:@"entries"]);
NSLog(@"sjson: %@", JSON);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error Code: %i - %@",[error code], [error localizedDescription]);
}];
When I go to http://mysiteurl/user/register/ directly I am able to see JSON.
What am I doing wrong?
If you’re doing this through a browser, you are making a
GETrequest, whereas in your code, you are making aPOSTrequest.A 404 is not just the visible address, it includes the HTTP method as well. You need to make sure that your server responds to a
POSTathttp://mysiteurl/user/register/. Depending on your framework (e.g. Rails), you may have to add[client setDefaultHeader:@"Accept" value:@"text/json"]to get the correct route.