0

I'm using AFNetworking to add Book objects to a server. Here's the setup:

Some dummy Book objects are created for testing:

Book *newBook1 = [[Book alloc] init];
newBook1.title = @"Test Book 1";
newBook1.author = @"Harlan";

Book *newBook2 = [[Book alloc] init];
newBook2.title = @"Test Book 2";
newBook2.author = @"Harlan";

NSArray *books = [NSArray arrayWithObjects:newBook1, newBook2, nil];

[bookCommunicator addBooks:books];

Inside BookCommunicator:

- (void)addBooks:(NSArray *)books
{
   for(Book *book in books)
   {
       [self addBook:book];
   }
}

- (void)addBook:(Book *)book
{
    NSDictionary *bookProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                book.author, @"author",
                                book.title, @"title",
                                nil];

    [_httpSessionManager POST:@"books" parameters:bookProperties
                  success:^(NSURLSessionDataTask *task, id responseObject) {
                      [self.delegate didAddBooks];
                  }
                  failure:^(NSURLSessionDataTask *task, NSError *error) {
                      [self.delegate addingBooksFailedWithError:error];
                  }
     ];
}

I get a success response. The responseObject is an array of all of the Book objects already on the server - excluding the new ones. Upon checking the server, these two new book objects are not there. Any ideas as to why?

H K
  • 1,215
  • 2
  • 16
  • 29
  • server-side problem seems more then possible in your case. – Rok Jarc Jan 31 '15 at 12:12
  • The server has a user interface - I'm able to make successful POST requests with that. Just not with my own code above – H K Jan 31 '15 at 12:13
  • hmm, what kind of formatting does your server expect? afnetworking serialiser should match that (for example: if server expects json, use proper serialiser: http://stackoverflow.com/a/21672071/653513) – Rok Jarc Jan 31 '15 at 12:17
  • I am using an AFJSONResponseSerializer. Seems to be fine - I'm able to do GET, PUT, and DELETE requests using a very similar pattern to the code listed – H K Jan 31 '15 at 12:19
  • response serialiser deals with response - you need to set request serialiser, too – Rok Jarc Jan 31 '15 at 12:22
  • `manager.requestSerializer = [AFJSONRequestSerializer serializer];` – Rok Jarc Jan 31 '15 at 12:23
  • That seemed promising - but alas, I'm still getting the same behavior – H K Jan 31 '15 at 12:40
  • there might be some API specifications issues: possibly the author & title should be enveloped within a "book" key, or the POST endpoint expects an array of book-dictionaries... do you have the server code at hand, or API specs? – Rok Jarc Jan 31 '15 at 12:55
  • I have the API specs. They say: POST /books Sample Params: author="Author Name" title="Book Title" Sample Response: Status 201 Created { "author": "Author Name", "title": "Book Title" } – H K Jan 31 '15 at 13:32
  • Seems like server expects HTTP request serialiser (even though it returns json)... (`[AFHTTPRequestSerializer serializer]`). If not, i'm out of ideas... – Rok Jarc Jan 31 '15 at 13:35
  • Still no luck! I'll post if I get it resolved – H K Jan 31 '15 at 21:46

1 Answers1

0

The issue was not with the code above, but with the API. It required a trailing / on the endpoint (i.e. 'books/') - but it should have accepted both 'books' or 'books/'.

H K
  • 1,215
  • 2
  • 16
  • 29