gpt4 book ai didi

restkit - 如何使用 RestKit 0.20.0 设置基本身份验证?

转载 作者:行者123 更新时间:2023-12-04 09:06:51 27 4
gpt4 key购买 nike

我正在尝试使用 RestKit 调用需要基本身份验证的端点。

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[JSNCategory class]];
[mapping addAttributeMappingsFromDictionary:@{
@"id": @"catId",
@"name": @"name"
}];

NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor
= [RKResponseDescriptor responseDescriptorWithMapping:mapping
pathPattern:@"/api/v1/categories"
keyPath:nil
statusCodes:statusCodes];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL
URLWithString:@"https://rest.example.com"]];

RKObjectRequestOperation *operation
= [[RKObjectRequestOperation alloc] initWithRequest:request
responseDescriptors:@[responseDescriptor]];

[operation setCompletionBlockWithSuccess:
^(RKObjectRequestOperation *operation, RKMappingResult *result) {
JSNCategory *cat = [result firstObject];
NSLog(@"Mapped the category: %@", cat);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Failed with error: %@", [error localizedDescription]);
}];

最佳答案

使用对象管理器,这将类似于:

NSURL* url = [[NSURL alloc]initWithString:@"http://rest.url.com"];
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:url];

[objectManager.HTTPClient setAuthorizationHeaderWithUsername:@"username" password:@"password"];

然后,在设置正确的请求/响应后,您可以使用对象管理器执行获取/发布/等操作:
[objectManager getObjectsAtPath:endpoint parameters:parameters success:
^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
// do something
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
// do something
}
];

关于restkit - 如何使用 RestKit 0.20.0 设置基本身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13853073/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com