使用 RestKit 自定义 MIME 类型
我一直在绞尽脑汁地思考在 RestKit 中选择哪些方法来实现我想要实现的目标。
我收到了一个 API 规范,要求我使用自定义 MIME 类型以 JSON 格式发布和对象,然后接收该对象的更新 JSON 表示形式并将其反序列化。
我尝试简单地使用 [[RKObjectManager sharedManager] postObject:customobject delegate:handler] 但是我无法让它使用除表单编码之外的任何内容。
然后,我尝试了 [[RKObjectManager sharedManager] objectLoaderForObject:customObject method:RKRequestMethodPost delegate:handler]
并将序列化 MIME 类型设置为 @"application/json"
。这适用于将对象序列化为 JSON,但对自定义内容类型没有帮助。
使用上面的第二种方法,我尝试实现 HTTPHeaderValueForContentType
消息,但这似乎从未被调用,并且 params 对象是一个 NSDictionary。
从那里我尝试使用与 [RKParseRegistry autoconfigure]
相同的逻辑添加自定义 MIME 类型解析器注册表,但我觉得我每一步都在深入挖掘黑客领域。
这种方法的问题也在于它对我的“Accepts”标头没有帮助,它仍然是“application/json
”。
以下是我需要使用的有关 REST API 的一些规范(更改了一些关键字)
注意: 我已将其交叉发布到 RestKit Google 群组
Request POST /customobject/ HTTP/1.1
Accepts application/vnd.company.customobject-v1+json
application/vnd.company.customobject-v1+xml
Content-Type application/vnd.company.customobject-v1+json
Sample content {
"type" : "type-a",
"direction" : "left",
"listofitems": [
{ "itemnumber": "123"}
]
}
Response HTTP/1.1 201 Created
Content-Type application/json
Location /customobject/[id]
I've been banging my head against a wall around which methods in RestKit to pick to fulfill what I'm trying to achieve.
I have been given an API specification that requires me to POST and object in JSON format with a custom MIME type and then receive the updated JSON representation of that object and deserialize it.
I have tried simply using [[RKObjectManager sharedManager] postObject:customobject delegate:handler] however I can't get this to use anything except form encoding.
I then tried [[RKObjectManager sharedManager] objectLoaderForObject:customObject method:RKRequestMethodPost delegate:handler]
and setting the serialization MIME type to @"application/json"
. This works for serializing the object as JSON but doesn't help with the custom Content-Type.
Using the second method above, I tried implementing the HTTPHeaderValueForContentType
message but this never seems to be called and the params object is an NSDictionary.
From there I've tried adding the custom MIME type the Parser Registry using the same logic as [RKParseRegistry autoconfigure]
but I feel that I'm digging deeper into hack territory with every step.
The problem with this approach too is that it doesn't help my "Accepts" header, it is still "application/json
".
Below is some specification about the REST API I need to consume (with some keywords changed)
NB: I have cross-posted this on the RestKit Google Group
Request POST /customobject/ HTTP/1.1
Accepts application/vnd.company.customobject-v1+json
application/vnd.company.customobject-v1+xml
Content-Type application/vnd.company.customobject-v1+json
Sample content {
"type" : "type-a",
"direction" : "left",
"listofitems": [
{ "itemnumber": "123"}
]
}
Response HTTP/1.1 201 Created
Content-Type application/json
Location /customobject/[id]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论