4

我正在尝试为我的 restkit 调用设置标头值。但是,这些似乎不起作用。

我想设置Content-TypeAccept标题application/json

知道问题出在哪里吗?

谢谢!

RKObjectManager *objectManager = [RKObjectManager sharedManager];
[[objectManager client] setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[[objectManager client] setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

RKURL *URL = [RKURL URLWithBaseURL:[objectManager baseURL] resourcePath:@"/users/sign_in.json" queryParameters:params];

[objectManager loadObjectsAtResourcePath:[NSString stringWithFormat:@"%@?%@", [URL resourcePath], [URL query]] delegate:self];
4

3 回答 3

9

我认为这不太明显,尽管它在文档中(我还没有尝试过),但在对象管理器上有一个serializationMIMEType属性。还已经定义了一个 JSON 常量,因此您的代码可能如下所示:

objectManager.serializationMIMEType = RKMIMETypeJSON;

根据文档, application/json 无论如何都是 Accept 的默认值,但可以由acceptMIMEType属性指定。

于 2012-07-06T23:46:19.370 回答
3

你有这个选项

[kGlobalObjectManager() loadObjectsAtResourcePath: @"path" usingBlock: ^(RKObjectLoader *loader) {
    NSDictionary* dictionary = [NSDictionary dictionaryWithObject: @"VALUE" forKey: @"HEADER NAME"];
    loader.additionalHTTPHeaders = dictionary;
    loader.delegate = self;
}];

更具体到你的问题[RKObjectManager setAcceptMIMEType:]

于 2012-07-09T06:52:36.430 回答
0

在类 RKObjectManager 中存在方法

**
 This method is the `RKObjectManager` analog for the method of the same name on `AFHTTPClient`.
 */
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
                                      path:(NSString *)path
                                parameters:(NSDictionary *)parameters
{
    NSMutableURLRequest* request;
    if (parameters && !([method isEqualToString:@"GET"] || [method isEqualToString:@"HEAD"] || [method isEqualToString:@"DELETE"])) {
        // NOTE: If the HTTP client has been subclasses, then the developer may be trying to perform signing on the request
        NSDictionary *parametersForClient = [self.HTTPClient isMemberOfClass:[AFHTTPClient class]] ? nil : parameters;
        request = [self.HTTPClient requestWithMethod:method path:path parameters:parametersForClient];

        NSError *error = nil;
        NSString *charset = (__bridge NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(self.HTTPClient.stringEncoding));
        [request setValue:[NSString stringWithFormat:@"%@; charset=%@", self.requestSerializationMIMEType, charset] forHTTPHeaderField:@"Content-Type"];
        NSData *requestBody = [RKMIMETypeSerialization dataFromObject:parameters MIMEType:self.requestSerializationMIMEType error:&error];
        [request setHTTPBody:requestBody];
    } else {
        request = [self.HTTPClient requestWithMethod:method path:path parameters:parameters];
    }

    return request;
}

这里存在内容类型的替代品,您可以在配置 RestKit 时尽早设置

[[[RKObjectManager sharedManager] HTTPClient] setDefaultHeader:@"Content-Type" value:@"application/json"];

为此,我找到了(不漂亮,但有效)解决方案,我做了分类

@interface RKObjectManager (BBContentType)

- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
                                      path:(NSString *)path
                                parameters:(NSDictionary *)parameters;

@end

@implementation RKObjectManager (BBContentType)

- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
                                      path:(NSString *)path
                                parameters:(NSDictionary *)parameters
{
    NSMutableURLRequest* request;
    if (parameters && !([method isEqualToString:@"GET"] || [method isEqualToString:@"HEAD"] || [method isEqualToString:@"DELETE"])) {
        // NOTE: If the HTTP client has been subclasses, then the developer may be trying to perform signing on the request
        NSDictionary *parametersForClient = [self.HTTPClient isMemberOfClass:[AFHTTPClient class]] ? nil : parameters;
        request = [self.HTTPClient requestWithMethod:method path:path parameters:parametersForClient];
        NSError *error = nil;
        NSData *requestBody = [RKMIMETypeSerialization dataFromObject:parameters MIMEType:self.requestSerializationMIMEType error:&error];
        [request setHTTPBody:requestBody];
    } else {
        request = [self.HTTPClient requestWithMethod:method path:path parameters:parameters];
    }

    return request;
}
@end

我只剪了两条线

NSString *charset = (__bridge NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(self.HTTPClient.stringEncoding));
[request setValue:[NSString stringWithFormat:@"%@; charset=%@", self.requestSerializationMIMEType, charset] forHTTPHeaderField:@"Content-Type"];
于 2015-04-12T05:21:02.923 回答