I'm trying to send a REST API to my server by AFNetworking2.0, our server only accepts Content-Type
as application/vnd.mycom.mycom-csc+json
when i send the request, it is json format indeed,
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);
}];
but it does not seem to be working, Content-Type
always be modified to application/json
in my request, who can help to solve this problem? many thanks.
Update: As of AFNetworking 2.2.3, all you need to do is:
[requestSerializer setValue:@"application/vnd.mycom.mycom-csc+json" forHTTPHeaderField:@"Content-Type"];
More detail in this Github issue.
In AFNetworking 2, serialization has been abstracted out into "request serializers" and "response serializers". The classes are designed to be subclassed to handle any special cases like yours.
For your specific question, the simplest approach would be:
AFJSONRequestSerializer
.Override the method that generates the URL Request like so:
- (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;
}
Tell your operation manager to use your newly created subclass:
operationMgr.requestSerializer = [MyAwesomeJSONRequestSerializer serializer];
I'm not sure if the behavior you're seeing is expected or not (the documentation doesn't match the implementation exactly). I opened an issue on Github to discuss it. Either way, the workaround I propose here should resolve the issue if you just want to get it working.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句