AFNetworking - 翻译cURL Put请求

时间:2016-04-12 21:53:50

标签: ios objective-c ios9 afnetworking afnetworking-3

这是使用cURL成功的PUT请求:

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

以下是我试图模仿的API文档:

enter image description here

我正在尝试使用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);
}];

但我一直收到这个错误:

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)}

在线阅读后,一切都说我没有处理正确的响应类型,所以我将其添加到:

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

但它仍然以同样的错误失败了。非常感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

从修复它的路线中删除“/”。所以:

[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);
}];

作品!

将来我应该仔细查看错误日志,它会提示我的格式错误的网址:NSErrorFailingURLKey=https://api.layer.com/badge

该行表示它是一个失败的URL,并且打印的URL不是完整的URL,所以我应该知道我搞砸了URL格式。