RESTKit 2.0: AFHTTPClient with text/html



我正在使用RESTKit v0.20.3。 由于我不期待响应对象,我决定使用 AFHTTPClient 来 POST。 我有以下代码:

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
httpClient.parameterEncoding = AFJSONParameterEncoding;
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[httpClient setDefaultHeader:@"Accept" value:@"application/json"];
[httpClient postPath:@"/update" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
   NSLog(@"AFJSONRequestOperation Alt response MIMEType %@",[responseObject MIMEType]);
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"***ERROR*****: %@", [error localizedDescription]);
}];

这是我的日志

2014-04-01 16:43:01.125 App[13181:60b] E restkit.network:RKObjectRequestOperation.m:209 POST 'http://api.domain.com' (200 OK) [0.1158 s]: Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {(
    "text/json",
    "application/json",
    "text/javascript"
)}, got text/html" UserInfo=0xccab6a0 {NSLocalizedRecoverySuggestion=<!DOCTYPE html><html><head><title></title><link rel="stylesheet" href="/stylesheets/style.css"></head><body><h1>Not Found</h1><h2>404</h2><pre>Error: Not Found
    at Object.handle (/var/app/current/app.js:175:15)
    at next (/var/app/current/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at pass (/var/app/current/node_modules/express/lib/router/index.js:110:24)
    at Router._dispatch (/var/app/current/node_modules/express/lib/router/index.js:173:5)
    at Object.router (/var/app/current/node_modules/express/lib/router/index.js:33:10)
    at next (/var/app/current/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at SessionStrategy.module.exports.strategy.pass (/var/app/current/node_modules/passport/lib/middleware/authenticate.js:314:9)
    at SessionStrategy.authenticate (/var/app/current/node_modules/passport/lib/strategies/session.js:61:12)
    at pass (/var/app/current/node_modules/passport/lib/authenticator.js:333:31)
    at deserialized (/var/app/current/node_modules/passport/lib/authenticator.js:345:7)</pre></body></html>, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0xda43280> { URL: http://api.domain.com/update }, NSErrorFailingURLKey=http://api.domain.com/update, NSLocalizedDescription=Expected content type {(
    "text/json",
    "application/json",
    "text/javascript"
)}, got text/html, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xdd50720> { URL: http://api.domain.com/update } { status code: 200, headers {
    "Content-Length" = 1083;
    "Content-Type" = "text/html; charset=utf-8";
    Date = "Tue, 01 Apr 2014 23:43:12 GMT";
    "Proxy-Connection" = "Keep-alive";
    Server = "nginx/1.4.3";
    "X-Powered-By" = Express;
} }}
2014-04-01 16:43:01.126 App[13181:60b] ***ERROR*****: Expected content type {(
    "text/json",
    "application/json",
    "text/javascript"
)}, got text/html

我收到错误是在接收text/html,但我如何告诉AFHTTPClient期望text/html

我尝试添加:

[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];

返回:

2014-04-01 16:48:13.110 App[13289:60b] ***ERROR*****: The operation couldn’t be completed. (Cocoa error 3840.)

如果我使用 AFHTTPRequestOperation ,如何将参数传递给它? 我正在使用RESTKit,所以我不能使用AFNetworking 2.0。 代码示例将不胜感激!

如果你检查HTML的内容,它实际上说:

<h1>Not Found</h1><h2>404</h2>

因此,问题不在于您请求的页面不存在,而在于返回的内容类型是错误的。

验证您正在使用的 URL 和路径。

相关内容

最新更新