gpt4 book ai didi

ios - 使用 RESTkit 将请求、更改 header 和保存在核心数据中的响应

转载 作者:行者123 更新时间:2023-11-29 13:21:01 26 4
gpt4 key购买 nike

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

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

以及以下 NSDictionary:

{"title": "Sumit"}

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

方法一:-

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

方法二:-

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

最佳答案

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

"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"];

关于ios - 使用 RESTkit 将请求、更改 header 和保存在核心数据中的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14357803/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com