如何设置除application/json以外的Content-Type [英] how to set Content-Type other than application/json

查看:403
本文介绍了如何设置除application/json以外的Content-Type的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试通过AFNetworking2.0将REST API发送到服务器,我们的服务器在发送请求时仅接受Content-Type作为application/vnd.mycom.mycom-csc+json,它的确是json格式,

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);
     }];

但它似乎不起作用,在我的请求中始终将Content-Type修改为application/json,谁能帮助解决此问题?非常感谢.

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.

推荐答案

更新:从AFNetworking 2.2.3开始,您需要做的是:

Update: As of AFNetworking 2.2.3, all you need to do is:

[requestSerializer setValue:@"application/vnd.mycom.mycom-csc+json" forHTTPHeaderField:@"Content-Type"];

此Github问题中的详细信息.

在AFNetworking 2中,序列化被抽象为请求序列化器"和响应序列化器".这些类被设计为可子类化,以处理像您这样的特殊情况.

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. 创建AFJSONRequestSerializer的子类.
  2. 重写生成URL请求的方法,如下所示:

  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;
}

  • 告诉您的运营经理使用您新创建的子类:

  • Tell your operation manager to use your newly created subclass:

    operationMgr.requestSerializer = [MyAwesomeJSONRequestSerializer serializer];
    

  • 我不确定您所看到的行为是否正常(文档与实现不完全匹配).我在Github上打开了一个问题以进行讨论.无论哪种方式,如果您只是想使它起作用,我在这里建议的解决方法都可以解决该问题.

    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.

    这篇关于如何设置除application/json以外的Content-Type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆