0

I am trying to POST a user to a rails backed server but I am getting an odd error. When I press submit, I get a 404 error in the log and the read out that says the page does not exist. Like so:

Error: Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 404" UserInfo=0xc03c140 {NSLocalizedRecoverySuggestion=<!DOCTYPE html>
<html>
<head>
  <title>The page you were looking for doesn't exist (404)</title>
  <style type="text/css">
    body { background-color: #fff; co...

Below, in the log it prints the failing url

AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0xc033300> { URL: http://example.herokuapp.com/users }, 

So the log clims that this page does not exist, yet if I copy and paste the url- the page does indeed exist, and I have already been able to post to it from the webview. For some reason afnetworking or the nsmutbleURLrequest is claiming the page doesn't exist, when it does... I mean I don't want to say that the computer is mistaken, but what's going on here? I've cleaned xcode and quit and nothing like that fixed this. Has anyone had this happen?

This is the code I am using to make the request:

+ (void)createUserWithUsername:(NSString *)signature
                         email:(NSString *)email
                      password:(NSString *)password
                         block:(void (^)(User *user))block
{
    NSDictionary *parameters = @{@"signature": signature,
                                 @"email": email,
                                 @"password": password
                                 };

    [[APIClient sharedClient] postPath:@"/users" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        User *user = [[User alloc] initWithDictionary:responseObject];

        if (block) {
            block(user);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        if (block) {
            block(nil);
        }
    }];
}

And this is what the json looks like from the succesful posts I've made from the web side

{"created_at":"2013-07-18T04:05:36Z","email":"grant@gmail.com","id":1,"signature":grant,"updated_at":"2013-07-18T04:05:36Z"}

So after using Charles to compare successful to failing post requests I see the failing posts get a 406 error. This error is probably correct because both successful and failing posts have the same post path. So I don't think its a 404 like it said before. But 406? In the log for the failing request there is no ref to a problem with the fact that its content-type is json.. This is the console log

headers {
    "Cache-Control" = "no-cache";
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Wed, 31 Jul 2013 17:24:53 GMT";
    "Proxy-Connection" = "Keep-alive";
    Server = "thin 1.5.1 codename Straight Razor";
    "Set-Cookie" = "_gold_session=BAh7Basdfasdk; path=/; HttpOnly";
    "Transfer-Encoding" = Identity;
    "X-Rack-Cache" = "invalidate, pass";
    "X-Request-Id" = c9c17573dde8b4767ba519aed6e04959;
    "X-Runtime" = "0.008262";
    "X-Ua-Compatible" = "IE=Edge,chrome=1";
} }}
2013-07-31 10:24:57.828 Gold[57543:a0b] User: (null)

Here is a comparison of the server logs from Charles, showing a failing post request and a successful one.

This is the request from a failing post from the iphone

    POST /users HTTP/1.1
Host    mysterious-5550.herokuapp.com
Content-Type    application/json; charset=utf-8
Accept-Encoding gzip, deflate
Content-Length  40
Accept-Language en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5
Accept  application/json
Connection  keep-alive
User-Agent  Gold/1.0 (iPhone Simulator; iOS 7.0; Scale/2.00)

This is the failing response:

HTTP/1.1 406 Not Acceptable
Cache-Control   no-cache
Content-Type    application/json; charset=utf-8
Date    Wed, 31 Jul 2013 18:26:12 GMT
Server  thin 1.5.1 codename Straight Razor
Set-Cookie  _gold_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFVEkiJTU2OGY2MWZhMzdmNzY2OGRjZGU0ZmQ1Y2Q5YmZiNzAxBjsAVA%3D%3D--3ab7521533c9e8f0f5b4a4d5da4d9f294bdeaba5; path=/; HttpOnly
X-Rack-Cache    invalidate, pass
X-Request-Id    900d86dd0719d0a00eacf803e90fa780
X-Runtime   0.007971
X-Ua-Compatible IE=Edge,chrome=1
transfer-encoding   chunked
Connection  keep-alive

This is the request of a successful post from the web

POST /users HTTP/1.1
Host    mysterious-5550.herokuapp.com
Content-Length  217
Cache-Control   max-age=0
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Origin  http://mysterious-5550.herokuapp.com
User-Agent  Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1580.0 Safari/537.36
Content-Type    application/x-www-form-urlencoded
Referer http://mysterious-5550.herokuapp.com/users/sign_up
Accept-Encoding gzip,deflate,sdch
Accept-Language en-US,en;q=0.8
Cookie  _gold_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFVEkiJTQ5MGY5MzMzMmNlMzkxZDI4NTllNDU0ZGQ0ZTE5MDg2BjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMStZRTNwbGRKQ1VJWXY4aXZtbUMybGt3ejFLcnBZSU5zY053WEcwa2kydDg9BjsARg%3D%3D--2dd8e808965ccc2da457abc94c58c39d080f62ab

This is the response from a successful post from the web

HTTP/1.1 302 Moved Temporarily
Cache-Control   no-cache
Content-Type    text/html; charset=utf-8
Date    Wed, 31 Jul 2013 18:30:11 GMT
Location    http://mysterious-5550.herokuapp.com/
Server  thin 1.5.1 codename Straight Razor
Set-Cookie  _gold_session=BAh7CUkiD3NlFlvdSBoY88c959; path=/; HttpOnly
X-Rack-Cache    invalidate, pass
X-Request-Id    c94b3d921dfe451c562c171524b78b3f
X-Runtime   0.750712
X-Ua-Compatible IE=Edge,chrome=1
transfer-encoding   chunked
Connection  keep-alive

Though the content-types are different (text/html v json) I don't think this is the problem. Both types should be accepted - and usually xcode would say unexpected content type. Can you see anything else that is incompatible that might be causing the 406 error?

  • 1
    Please post the code you're using to make the request. Also, please post the output of `po yourMutableURLRequest` on the line before you create your AFHTTPRequestOperation object. – Aaron Brager Jul 31 '13 at 01:56
  • Could it be because it is POST in the app, and GET in the browser? – Khanh Nguyen Jul 31 '13 at 02:40
  • @KhanhNguyen he said, "I have already been able to post to it from the webview" – Aaron Brager Jul 31 '13 at 03:53
  • @AaronBrager I added some more info as per your request. Thanks for the help – grantvansant Jul 31 '13 at 15:16
  • You should use a proxy debugger—like Charles Proxy or Fiddler—to see what is going on with your request in the success and failure case. Whatever is different is the clue you need. – i_am_jorf Jul 31 '13 at 15:33

2 Answers2

0

This code looks fine.

First, go into the postPath:parameters:success:failure: method and set a breakpoint before the last line. Type po request in the console and make sure the URL matches what you expect.

If the URL isn't what you expect, check your initWithBaseURL call and make sure you want that leading slash before "users".

If the URL is what you expect, you need to debug with your server administrator because the server likely needs some other info like authentication or Accept headers. Note that 404 is not the correct status code to return in this case, so you may want to open a discussion about when you use which status codes.

https://github.com/AFNetworking/AFHTTPRequestOperationLogger will help a lot with this - it can log your requests and what they return to the console.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • After monitoring a failing post on Charles I see I'm getting a 406 error. I added some of the logs above – grantvansant Jul 31 '13 at 17:48
  • 406 indicates you probably need to set one of the `Accept` headers differently (as I mentioned above.) See a list here: http://stackoverflow.com/a/8410669/1445366 – Aaron Brager Jul 31 '13 at 18:01
  • I posted a comparison of the request and response headers. Do you see anything that might cause the 406? – grantvansant Jul 31 '13 at 23:24
  • @grantvansant - There are a number of differences. You'll have to ask your server admin or go trial and error. I'd start with the headers that begin with `Accept` and `Content`. – Aaron Brager Aug 01 '13 at 17:09
  • Is your server set up to return JSON? It looks like you're requesting JSON from iOS but requesting XML from the web. – Aaron Brager Aug 01 '13 at 17:09
  • actually do you think this has something to do with the Devise configuration? I probably should have mentioned, on the rails side, I use Devise for authentication – grantvansant Aug 01 '13 at 23:43
-2

Server application may be requiring some more info like cookies and returning 404.

cahn
  • 1,350
  • 1
  • 12
  • 24