我正在尝试通过 AFNetworking2.0 向我的服务器发送 REST API,我们的服务器只接受Content-Type
as application/vnd.mycom.mycom-csc+json
当我发送请求时,它确实是json格式,
self.operationMgr = [AFHTTPRequestOperationManager manager];
self.operationMgr.securityPolicy.allowInvalidCertificates = YES;
operationMgr.responseSerializer = [AFJSONResponseSerializer serializer];
operationMgr.requestSerializer = [AFJSONRequestSerializer serializer];
self.operationMgr.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/vnd.mycom.mycom-csc+json"];
[self.operationMgr.requestSerializer setValue:@"application/vnd.mycom.mycom-csc+json" forHTTPHeaderField:@"Accept"];
[self.operationMgr.requestSerializer setValue:@"application/vnd.mycom.mycom-csc+json" forHTTPHeaderField:@"Content-Type"];
[self.operationMgr POST:@"https://ip/rest" parameters:body
success:^(AFHTTPRequestOperation *operation, id responseObject) {
}
failure:^(AFHTTPRequestOperation* task, NSError* error){
NSLog(@"Error: %@", error);
}];
但它似乎不起作用,Content-Type
总是被修改为application/json
在我的请求中,谁能帮忙解决这个问题?非常感谢。
Update:从 AFNetworking 2.2.3 开始,您需要做的就是:
[requestSerializer setValue:@"application/vnd.mycom.mycom-csc+json" forHTTPHeaderField:@"Content-Type"];
更多详细信息,请参阅这个 Github 问题.
在 AFNetworking 2 中,序列化已被抽象为“请求序列化器”和“响应序列化器”。这些类被设计为子类化,以处理像您这样的任何特殊情况。
对于您的具体问题,最简单的方法是:
- 创建一个子类
AFJSONRequestSerializer
.
-
重写生成 URL 请求的方法,如下所示:
- (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request
withParameters:(id)parameters
error:(NSError *__autoreleasing *)error {
NSMutableURLRequest *mutableRequest = [[super requestBySer…etc.] mutableCopy];
[mutableRequest setValue:@"application/vnd.mycom.mycom-csc+json" forHTTPHeaderField:@"Content-Type"];
return mutableRequest;
}
-
告诉您的运营经理使用您新创建的子类:
operationMgr.requestSerializer = [MyAwesomeJSONRequestSerializer serializer];
我不确定您所看到的行为是否符合预期(文档与实现不完全匹配)。我在 Github 上打开了一个问题来讨论它。不管怎样,如果您只是想让它正常工作,我在这里提出的解决方法应该可以解决问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)