Search code examples
iosobjective-cios9afnetworkingafnetworking-3

AFNetworking - Translating cURL Put request


This is the PUT request that is succeeding with cURL:

curl -X PUT \
-H 'Accept: application/vnd.layer+json; version=1.1' \
-H 'Authorization: Bearer XXXXXXXXXXX' \
-H 'Content-Type: application/json' \
-d '{"external_unread_count": 13}' \
https://api.layer.com/apps/XXXXX-XXXXX-XXXXX-XXXXX-XXXXXXX/users/XXXXXXX/badge

Here is the API documentation I am trying to mimick:

enter image description here

I am trying to send the same request using AFNetworking:

NSDictionary *params = @{@"external_unread_count": @13};
NURL *baseURL = [NSURL URLWithString: @"https://api.layer.com/apps/XXXXX-XXXX-XXXX-XXXX-XXXXXXXX/users/XXXXXXX""];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:baseUrl] sessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.requestSerializer setValue:@"application/vnd.layer+json; version=1.1" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:@"Bearer XXXXXXXXXXX" forHTTPHeaderField:@"Authorization"];
[manager PUT:@"/badge"
         parameters:params
         success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                      NSLog(@"put successful!");
                }
         failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                      NSLog(@"put error: %@", error);
}];

But I consistently receive this error:

Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unacceptable (406)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x125883100> { URL: https://api.layer.com/badge } { status code: 406, headers {
    Connection = "keep-alive";
    "Content-Length" = 227;
    "Content-Type" = "application/json";
    Date = "Tue, 12 Apr 2016 20:17:49 GMT";
    Server = "nginx/1.8.0";
} }, NSErrorFailingURLKey=https://api.layer.com/badge, com.alamofire.serialization.response.error.data=<7b22636f 6465223a 3130372c 22696422 3a22696e 76616c69 645f6865 61646572 222c226d 65737361 6765223a 22496e76 616c6964 20616363 65707420 68656164 65723b20 6d757374 20626520 6f662074 68652066 6f726d20 27617070 6c696361 74696f6e 2f766e64 2e6c6179 65722b6a 736f6e3b 20766572 73696f6e 3d782e79 272e222c 2275726c 223a2268 74747073 3a2f2f64 6576656c 6f706572 2e6c6179 65722e63 6f6d2f64 6f63732f 636c6965 6e742f72 65737423 696e7661 6c69645f 68656164 6572222c 22646174 61223a7b 22686561 64657222 3a226163 63657074 227d7d>, NSLocalizedDescription=Request failed: unacceptable (406)}

After reading online, everything said that I wasn't handling the correct response types, so I added this in:

[manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/plain", @"text/html", nil]];

But it still failed with the same error. Any help would be greatly appreciated thank you!


Solution

  • Removing the "/" from the route fixed it. So:

    [manager PUT:@"badge"
             parameters:params
             success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                          NSLog(@"put successful!");
                    }
             failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                          NSLog(@"put error: %@", error);
    }];
    

    works!

    In the future I should've looked at the error log closer, it gave a hint as to my malformed url with the line: NSErrorFailingURLKey=https://api.layer.com/badge

    That line says it is a failing URL and the URL printed isn't the full URL so I should've known that I messed up the URL format.