API 密钥和 RESTKit

发布于 2024-12-09 17:39:58 字数 776 浏览 1 评论 0原文

我正在将 RESTKit 用于为我创建的服务。我的用户的 JSON 模型是:

{
    "api_key" : "123456"
    "user" : {
        "username" : "user1",
        "email"    : "[email protected]",
        "name"     : "name1",
        "surname"  : "surname1"
    }
}

如果我想获取用户 /users/1.json,我需要引入 api_key。我在文档中阅读了有关设置应用程序范围的 API 密钥 HTTP 标头的信息。

例子是:

[client setValue:@"123456" forHTTPHeaderField:@"X-RESTKIT-API-KEY"];

这样做的方法正确吗?如何发现我的服务的 @"X-RESTKIT-API-KEY"? (不是 api_key 值,我搜索字符串来替换我的服务的 @"X-RESTKIT-API-KEY"

I'm using RESTKit for a service that was created for me. The model in JSON for my user is:

{
    "api_key" : "123456"
    "user" : {
        "username" : "user1",
        "email"    : "[email protected]",
        "name"     : "name1",
        "surname"  : "surname1"
    }
}

I need introduce the api_key if I want to GET an user /users/1.json. I read in the documentation about set an app-wide API key HTTP header.

The example is:

[client setValue:@"123456" forHTTPHeaderField:@"X-RESTKIT-API-KEY"];

Is it the right way to do this? How can I discover my @"X-RESTKIT-API-KEY" of my service? (not api_key value, I search the string to replace @"X-RESTKIT-API-KEY" for my service)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角迷惘 2024-12-16 17:39:58

嗯,对于该示例 GET 请求,我建议您使用 RKClient 对象。我会给你一些例子:

#import <RestKit/RestKit.h>
#import "JSONKit.h"

RKClient* client = [RKClient clientWithBaseURL:@"http://yourdomainwhereisapi.com"]; 

在你的应用程序中的任何地方(因为 RKCLient 是单例对象)你都可以调用:

// Here you need to define delegate in header (because delegate is self)
// Declare delegate in .h file with <RKRequestDelegate>
[[RKClient sharedClient] get:@"/some/api/call.json" delegate:self];

你必须定义委托方法:

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
      // where you handle response object
      // sample how to make NSDictionary from JSON response
      NSDictionary *deserializedData = [[response bodyAsString] objectFromJSONString];

      // Now you can get anything from this NSDictionary:
      NSLog("%@", [deserializedData objectForKey:@"api_key"];
}

PS无论如何,你可以看看 RestKit wiki 以查看基本示例:RestKit Wiki 教程

Hmm, for that sample GET request, I suggest you to use RKClient object. I will give you some example:

#import <RestKit/RestKit.h>
#import "JSONKit.h"

RKClient* client = [RKClient clientWithBaseURL:@"http://yourdomainwhereisapi.com"]; 

and anywhere in you app (because RKCLient is singleton object) you can call:

// Here you need to define delegate in header (because delegate is self)
// Declare delegate in .h file with <RKRequestDelegate>
[[RKClient sharedClient] get:@"/some/api/call.json" delegate:self];

You must to define delegate method:

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
      // where you handle response object
      // sample how to make NSDictionary from JSON response
      NSDictionary *deserializedData = [[response bodyAsString] objectFromJSONString];

      // Now you can get anything from this NSDictionary:
      NSLog("%@", [deserializedData objectForKey:@"api_key"];
}

P.S. Anyway, you can take a look at RestKit wiki to see basic examples: RestKit Wiki tutorial

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文