1

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

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

以及以下内容NSDictionary

{"title": "Sumit"}

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

方法1:-

- 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
4

1 回答 1

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"];
于 2013-01-16T14:21:26.337 回答