简体   繁体   English

AFNetworking请求失败:不可接受的内容类型:text / html

[英]AFNetworking Request failed: unacceptable content-type: text/html

I'm trying to send POST request to server, and I'm still failing to do so. 我正在尝试向服务器发送POST请求,但我仍然没有这样做。 Please help me out what's incorrect. 请帮我解决不正确的问题。

Shared instance init: 共享实例init:

- (id)init{
self = [self initWithBaseURL:[NSURL URLWithString:kBaseURL]];
if(self) {
    self.
    self.responseSerializer = [AFJSONResponseSerializer serializer];
    [self.requestSerializer setAuthorizationHeaderFieldWithUsername:@"user" password:@"password"];
    parsingQueue = dispatch_queue_create("com.company.queue.parser",NULL);
}
return self;

post method body: 帖子方法体:

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:[FSApplicationManager sharedInstance].userToken, @"user_token", nil];
[params setObject:[NSNumber numberWithDouble:userId] forKey:@"user_id"];
[params setObject:[NSNumber numberWithDouble:filmId] forKey:@"film_id"];
[params setObject:[NSNumber numberWithBool:NO] forKey:@"sendCopy"];

if([subject length]>0){
    [params setObject:subject forKey:@"subject"];
}
[params setObject:message forKey:@"message"];

NSString *URLString = [NSString stringWithFormat:@"%@%@", kBaseURL, kAPISendMessageToUser];

NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"POST"
                                                               URLString:URLString
                                                              parameters:params
                                                                   error:nil];

[request addValue:[self signatureWithURL:[request.URL absoluteString] requestBody:[[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]] forHTTPHeaderField:@"X-SERVAPI-Signature"];
[request addValue:pub forHTTPHeaderField:@"X-SERVAPI-PublicKey"];

AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id response){
    NSLog(@"%@", response);

} failure:^(AFHTTPRequestOperation *operation,  NSError *error){
    NSLog(@"%@, \n response %@", error, operation.responseObject);
}];

[self.operationQueue addOperation:operation];

return operation;

error I'm getting: 错误我得到:

Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: internal server error (500)" UserInfo=0xcf0c2f0 {NSErrorFailingURLKey= https://ppd.someserver.com/api/user/send-message , NSLocalizedDescription=Request failed: internal server error (500), NSUnderlyingError=0xcf06ce0 "Request failed: unacceptable content-type: text/html", AFNetworkingOperationFailingURLResponseErrorKey= { URL: https://ppd.someserver.com/api/user/send-message } { status code: 500, headers { Connection = close; 错误域= AFNetworkingErrorDomain代码= -1011“请求失败:内部服务器错误(500)”UserInfo = 0xcf0c2f0 {NSErrorFailingURLKey = https://ppd.someserver.com/api/user/send-message,NSLocalizedDescription =请求失败:内部服务器error(500),NSUnderlyingError = 0xcf06ce0“请求失败:不可接受的内容类型:text / html”,AFNetworkingOperationFailingURLResponseErrorKey = {URL: https ://ppd.someserver.com/api/user/send-message} {status code:500 ,标题{Connection = close; "Content-Encoding" = gzip; “Content-Encoding”= gzip; "Content-Length" = 1412; “内容长度”= 1412; "Content-Type" = "text/html; charset=UTF-8"; “Content-Type”=“text / html; charset = UTF-8”; Date = "Sun, 14 Dec 2014 13:46:57 GMT"; 日期=“太阳报,2014年12月14日13:46:57 GMT”; P3P = "CP=\\"NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM\\""; P3P =“CP = \\”NOI ADM DEV PSAi COM NAV OURo STP IND DEM \\“”; Server = Apache; 服务器= Apache; Vary = "Accept-Encoding"; Vary =“Accept-Encoding”; "X-Powered-By" = "PHP/5.3.3-7+squeeze19"; “X-Powered-By”=“PHP / 5.3.3-7 + squeeze19”; } }}, },},

I've tried to contact guy responsible for server and that is what I get: 我试图联系负责服务器的人,这就是我得到的:

There is no need to add the content type to your request but if you wish to do so, since your are using a POST method, you should use: Content-Type: application/x-www-form-urlencoded; 没有必要在您的请求中添加内容类型,但如果您希望这样做,因为您使用的是POST方法,您应该使用:Content-Type:application / x-www-form-urlencoded;

The problem is that the server is sending you an HTML encoded answer. 问题是服务器正在向您发送HTML编码的答案。 You can do one of these 2 options: 您可以执行以下两个选项之一:

  1. Talk to the server side people, to encode it as a json. 与服务器端人员交谈,将其编码为json。

  2. Attempt to fix it by yourself by doing the following: After setting your self.responseSerializer put this line: 通过执行以下操作尝试自行修复:设置self.responseSerializer后,将此行放入:

    self.operationManager.responseSerializer.acceptableContentTypes = [self.operationManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

This line adds "text/html" to the acceptable types. 此行将“text / html”添加到可接受的类型中。 Providing that server's response is still serializable - you should be ok. 提供服务器的响应仍然是可序列化的 - 你应该没问题。

You are receiving HTML response. 您正在接收HTML响应。 To diagnose what's going on, it's useful to see what that HTML actually says. 要诊断正在发生的事情,查看HTML实际说明的内容很有用。

Temporarily change the response serializer to AFHTTPResponseSerializer and then examine the HTML response and it might provide some information that will enlighten you regarding what's wrong with the request. 暂时将响应序列化程序更改为AFHTTPResponseSerializer ,然后检查HTML响应,它可能会提供一些信息,以帮助您了解请求的错误。 Or you could leave the code alone and observe the exchange with Charles or other similar tool. 或者您可以单独留下代码并观察与Charles或其他类似工具的交换。 But the goal is to see why you got a status code of 500 and the HTML a might give you some clues. 但目标是了解为什么你的状态代码为500,HTML a 可能会给你一些线索。

Regarding why the request isn't working, it's hard for us to say without seeing the API. 关于为什么请求不起作用,我们很难说没有看到API。 The mix of key name styles in the request is suspicious (underscores in some, camelCase in others), but we don't know the API, so it's not clear if that is a problem or not. 请求中关键名称样式的混合是可疑的(在某些情况下是下划线,在其他情况下是camelCase),但我们不知道API,因此不清楚这是否是一个问题。

Regarding the Content-Type , AFNetworking will do that for you (and will use application/x-www-form-urlencoded , so I don't think that's the issue. 关于Content-Type ,AFNetworking将为您做到这一点(并将使用application/x-www-form-urlencoded ,所以我不认为这是问题。

If the API support team isn't familiar with Objective-C/AFNetworking code samples, you might want to observe the request using a tool such as Charles or something like that. 如果API支持团队不熟悉Objective-C / AFNetworking代码示例,您可能希望使用诸如Charles之类的工具来观察请求。 Then capture the raw request getting sent and perhaps the API support team can better diagnose the problem without the distraction of the Objective-C/AFNetworking code. 然后捕获发送的原始请求,并且API支持团队可以更好地诊断问题,而不会分散Objective-C / AFNetworking代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AFNetworking 请求失败:不可接受的内容类型:文本/纯文本, - AFNetworking Request failed: unacceptable content-type: text/plain, 使用AFNetworking 2.0的“请求失败:不可接受的内容类型:text / html” - “Request failed: unacceptable content-type: text/html” using AFNetworking 2.0 请求失败:不可接受的内容类型:使用AFNetworking 2.0的text / html - Request failed: unacceptable content-type: text/html using AFNetworking 2.0 请求失败:不可接受的内容类型:使用AFNetworking 2.0的文本/纯文本 - Request failed: unacceptable content-type: text/plain using AFNetworking 2.0 请求失败:不可接受的内容类型:文本/纯文字-AFNetwork POST - Request failed: unacceptable content-type: text/plain - AFNetwork POST Swift项目中的AFNetorking-“错误:请求失败:内容类型不可接受:text / html” - AFNetorking in Swift project — “Error: Request failed: unacceptable content-type: text/html” 请求失败:不可接受的内容类型:使用AFNetworking 2.5的图像/ jpg - Request failed: unacceptable content-type: image/jpg using AFNetworking 2.5 AFNetworking 2.0 - “不可接受的内容类型:text / plain” - AFNetworking 2.0 - “unacceptable content-type: text/plain” 具有文本/纯文本响应的API失败,显示“内容类型不可接受” - API with text/plain response failed with 'unacceptable content-type' 使用AFNetworking解析Content-Type text / html - Parsing Content-Type text/html with AFNetworking
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM