我想向服务器发送请求,带有以下请求标头的PUT请求:
Content-Type: application/json; charset=UTF-8
以下NSDictionary
:
{"title": "Sumit"}
我正在使用reskit发出put请求,发出了put请求,但是提供了与header相关的错误。
我想知道在向远程服务器发送请求时如何设置标头和字典。
此外,对于此任务,哪种方法最适合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
答案 0 :(得分: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"];