how to set Content-Type other than application/json

user2159851

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.

Aaron Brager

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.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How to set text content with SimpleXML's XPath support?

来自分类Dev

How to declare an array type that can contain either a string or other array?

来自分类Dev

How to set type parameter bound in scala to make generic function for numerics?

来自分类Dev

Querying DocumentDB using a property other than Id

来自分类Dev

Set string variable content in princexml

来自分类Dev

Unity3D C#: How to resize an inputfield according the content which is set dynamic

来自分类Dev

ErrorDocument中的Content-Type

来自分类Dev

覆盖默认的 Content-Type

来自分类Dev

How to set my activity properties to avoid interrupting other apps when my app comes in foreground

来自分类Dev

How can I set up a WiFi-hotspot to host files which can be downloaded by an other device later

来自分类Dev

unordered_map <TYPE,bool>与set <TYPE>

来自分类Dev

How to get the previous row value in the result set of type forward only using Java

来自分类Dev

Set button content on button click trigger in xaml

来自分类Dev

Set image content from ajax response

来自分类Dev

Programmatically set WinRt button content to image

来自分类Dev

无法设置Content-Type标头

来自分类Dev

未设置PHP cURL Content-Type

来自分类Dev

HttpClient不返回Content-Type

来自分类Dev

强制Nginx发送特定的Content-Type

来自分类Dev

Determine type of requested content inside doGet

来自分类Dev

回形针+ RSpec:content_type验证

来自分类Dev

header(“ Content-type:application / json”);`的用法

来自分类Dev

获取图像的content-type =“ text / plain”?

来自分类Dev

XMLHttpRequest / ajax设置Content-Type

来自分类Dev

从HEAD请求输出Content-Type

来自分类Dev

未设置PHP cURL Content-Type

来自分类Dev

为什么需要Content / Type变量?

来自分类Dev

获取图像的content-type =“ text / plain”?

来自分类Dev

设置 Content-Type 标头

Related 相关文章

  1. 1

    How to set text content with SimpleXML's XPath support?

  2. 2

    How to declare an array type that can contain either a string or other array?

  3. 3

    How to set type parameter bound in scala to make generic function for numerics?

  4. 4

    Querying DocumentDB using a property other than Id

  5. 5

    Set string variable content in princexml

  6. 6

    Unity3D C#: How to resize an inputfield according the content which is set dynamic

  7. 7

    ErrorDocument中的Content-Type

  8. 8

    覆盖默认的 Content-Type

  9. 9

    How to set my activity properties to avoid interrupting other apps when my app comes in foreground

  10. 10

    How can I set up a WiFi-hotspot to host files which can be downloaded by an other device later

  11. 11

    unordered_map <TYPE,bool>与set <TYPE>

  12. 12

    How to get the previous row value in the result set of type forward only using Java

  13. 13

    Set button content on button click trigger in xaml

  14. 14

    Set image content from ajax response

  15. 15

    Programmatically set WinRt button content to image

  16. 16

    无法设置Content-Type标头

  17. 17

    未设置PHP cURL Content-Type

  18. 18

    HttpClient不返回Content-Type

  19. 19

    强制Nginx发送特定的Content-Type

  20. 20

    Determine type of requested content inside doGet

  21. 21

    回形针+ RSpec:content_type验证

  22. 22

    header(“ Content-type:application / json”);`的用法

  23. 23

    获取图像的content-type =“ text / plain”?

  24. 24

    XMLHttpRequest / ajax设置Content-Type

  25. 25

    从HEAD请求输出Content-Type

  26. 26

    未设置PHP cURL Content-Type

  27. 27

    为什么需要Content / Type变量?

  28. 28

    获取图像的content-type =“ text / plain”?

  29. 29

    设置 Content-Type 标头

热门标签

归档