0

这是使用 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 文档:

在此处输入图像描述

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

但它仍然因同样的错误而失败。任何帮助将不胜感激,谢谢!

4

1 回答 1

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

作品!

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

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

于 2016-04-12T22:03:10.887 回答