如何在Restkit中设置标头值

时间:2012-07-06 23:35:40

标签: ios restkit

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

3 个答案:

答案 0 :(得分:9)

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

objectManager.serializationMIMEType = RKMIMETypeJSON;

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

答案 1 :(得分:3)

您有此选项

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

您的问题更具针对性[RKObjectManager setAcceptMIMEType:]

答案 2 :(得分: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"];