使用 RESTkit PUT 请求、更改标头并将响应保存在核心数据中

Posted

技术标签:

【中文标题】使用 RESTkit PUT 请求、更改标头并将响应保存在核心数据中【英文标题】:PUT request , change headers and save response in core data using RESTkit 【发布时间】:2013-01-16 11:51:17 【问题描述】:

我想向服务器发送一个请求,一个带有以下请求标头的 PUT 请求:

Content-Type: application/json; charset=UTF-8

还有下面NSDictionary

"title": "Sumit"

我正在使用 reskit 发出 put 请求,发出了 put 请求但给出了与标头相关的错误。 我想知道在向远程服务器发送请求时如何设置标头和字典。 此外,对于此任务,RKObjectManager 中哪种方法最好:-

方法一:-

- NSMutableURLRequest *request = [manager requestWithObject:newImage
                                                   method:RKRequestMethodPUT
                                                     path:[kImageUrl stringByAppendingString:imageUrl]
                                               parameters:jsonParameters];

方法2:-

- (NSMutableURLRequest *)multipartFormRequestWithObject:(id)object
                                                 method:(RKRequestMethod)method
                                                   path:(NSString *)path
                                             parameters:(NSDictionary *)parameters
                              constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block;

我得到的错误:-

(400 Bad Request) [0.4221 s]: Error Domain=AFNetworkingErrorDomain Code=-1016 
"Expected content type (
   "application/x-www-form-urlencoded",
   "application/json"
), got text/plain" UserInfo=0xa0951c0 NSLocalizedRecoverySuggestion=Bad Request,
AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest  
http://staging.zoomdeck.com/api/image/c5ot31sxnh8v/>, 
NSErrorFailingURLKey=http://staging.zoomdeck.com/api/image/c5ot31sxnh8v/,  
NSLocalizedDescription=Expected content type (
    "application/x-www-form-urlencoded",
    "application/json"
), got text/plain, AFNetworkingOperationFailingURLResponseErrorKey=<
NSHTTPURLResponse: 0xa194530>
2013-01-16 17:19:43.860 Zoomdeck[2905:4a07] E 
restkit.network:RKObjectRequestOperation.m:285 Object request failed: 
Underlying HTTP request operation failed with error: Error 
Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type (
    "application/x-www-form-urlencoded",
    "application/json"
), got text/plain" UserInfo=0xa0951c0 NSLocalizedRecoverySuggestion=Bad Request,  
AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest 
http://staging.zoomdeck.com/api/image/c5ot31sxnh8v/>, 
NSErrorFailingURLKey=http://staging.zoomdeck.com/api/image/c5ot31sxnh8v/, 
NSLocalizedDescription=Expected content type (
    "application/x-www-form-urlencoded",
    "application/json"
), got text/plain, AFNetworkingOperationFailingURLResponseErrorKey=
<NSHTTPURLResponse: 0xa194530>
2013-01-16 17:19:43.861 Zoomdeck[2905:c07] Expected content type (
    "application/x-www-form-urlencoded",
    "application/json"
), got text/plain

【问题讨论】:

【参考方案1】:

您的休息服务需要内容类型

"application/x-www-form-urlencoded"

"application/json"

但它获取内容类型

text/plain

您必须像这样为您的请求设置 contentType:

NSMutableURLRequest *request = [manager requestWithObject:newImage
                                               method:RKRequestMethodPUT
                                                 path:[kImageUrl stringByAppendingString:imageUrl]
                                           parameters:jsonParameters];



[request setValue:@"application/json" forHTTPHeaderField:@"content-type"];

【讨论】:

谢谢,我尝试了很多这样的方法......但是在restkit内部处理是通过某种方式完成的,因此我发现,我们可以在制作这样的 NSMutableRequest 对象之前设置它. manager.requestSerializationMIMEType = RKMIMETypeJSON;它现在可以通过这个...

以上是关于使用 RESTkit PUT 请求、更改标头并将响应保存在核心数据中的主要内容,如果未能解决你的问题,请参考以下文章

RestKit 为所有请求添加自定义标头?

将请求特定的 HTTP 标头与 RestKit 一起使用

RestKit 在 PUT 请求时抛出 NSInternalInconsistencyException

如何将 PUT 请求添加到 RestKit + Core Data?

如何在 Restkit 中执行没有正文、url 参数的 PUT 请求并取回对象

RestKit 查看 RKObjectRequestOperation 的所有标头