AFNetworking版本2内容类型错误 [英] AFNetworking version 2 content-type error

查看:109
本文介绍了AFNetworking版本2内容类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个将与特定JIRA服务器进行交互的iphone应用。我有以下代码可登录:

I'm attempting to make an iphone app that will interact with a particular JIRA server. I've got the following code to log in:

NSURL *url = [[NSURL alloc] initWithString:@"https://mycompany.atlassian.net/rest/auth/latest/session/"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *postString = [NSString stringWithFormat:@"{\"username\":\"%@\",\"password\":\"%@\"}", username, password];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept" ];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:
    ^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"ERROR: %@", error);

    }
 ];
 [operation start];

但这给我以下与Content-Type有关的错误:

But it's giving me the following error having to do with Content-Type:

ERROR: Error Domain=AFNetworkingErrorDomain Code=-1011
"Request failed: unsupported media type (415)"
UserInfo=0x8cd6540
{
  NSErrorFailingURLKey=https://mycompany.atlassian.net/rest/auth/latest/session/,
  NSLocalizedDescription=Request failed: unsupported media type (415),
  NSUnderlyingError=0x8c72e70
  "Request failed: unacceptable content-type: text/html", 

我不确定是什么问题。我发现了 这个问题 ,我认为这可能是一个类似的问题,但是答案说要么使用 AFJSONRequestOperation 类(之所以不能,因为我使用的是AFNetworking版本2,它没有该类),或将其修复在服务器端(出于明显的原因,我也无法这样做)。

I'm not sure what the problem is. I found this question, which I thought might be a similar problem, but the answers say to either use the AFJSONRequestOperation class (which I can't because I'm using AFNetworking version 2, which doesn't have that class), or to fix it on the server side (which I also can't for obvious reasons).

当我无法修复服务器端时,该如何解决此错误?并且我不能使用 AFJSONRequestOperation

What can I fix this error when I can't fix the server side and I can't use AFJSONRequestOperation?

推荐答案

如果使用AFNetworking 2.0 ,您可以使用 POST 方法,将其简化一下:

If using AFNetworking 2.0, you can use the POST method, which simplifies this a bit:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *parameters = @{@"username":username, @"password":password};
[manager POST:@"https://mycompany.atlassian.net/rest/auth/latest/session/" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

这将创建请求,并设置其 Content-Type 根据 requestSerializer 设置,并为您编码JSON。 AFNetworking的优点之一是您可以摆脱手动构造和配置 NSURLRequest 对象的麻烦。

This does the creation of the request, setting its Content-Type according to the requestSerializer setting, and encodes the JSON for you. One of the advantages of AFNetworking is that you can get out of the weeds of constructing and configuring NSURLRequest objects manually.

顺便说一句,请求失败:不可接受的内容类型:文本/ html错误意味着无论您希望收到什么(例如JSON),您都将收到HTML响应。这很常见:许多服务器错误(例如,服务器通知您请求格式错误等)会生成HTML错误消息。如果要查看HTML,请在失败块中,只需记录 operation.responseString

By the way, the "Request failed: unacceptable content-type: text/html" error means that regardless of what you were expecting to receive (e.g. JSON), you received HTML response. This is very common: Many server errors (e.g. the server informing you that the request was malformed, etc.) generate HTML error messages. If you want to see that HTML, in your failure block, simply log the operation.responseString.

这篇关于AFNetworking版本2内容类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆