how to set Content-Type other than application/json

强颜欢笑 提交于 2019-12-31 00:00:48

问题


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.


回答1:


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:

  1. Create a subclass of AFJSONRequestSerializer.
  2. 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;
    }
    
  3. 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.



来源:https://stackoverflow.com/questions/22146459/how-to-set-content-type-other-than-application-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!