AFNetworking 2.0 Request w. Custom Header

前端 未结 2 917
轻奢々
轻奢々 2021-02-14 07:58

Ive tried to avoid asking such a newb question on here, but im a Android dev learning IOS and I cant figure out for the life of me how to add a simple header to my post requests

2条回答
  •  梦毁少年i
    2021-02-14 08:29

    Under AFHTTPRequestOperationManager you will notice a property called requestSerializer. This is of type AFHTTPRequestSerializer, and requests made through the HTTP manager are constructed with the headers specified by this object.

    So, try this:

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    [manager.requestSerializer setValue:@"SomeValue" forHTTPHeaderField:@"SomeHeaderField"]
    
    //Make your requests
    

    You can read the headers dictionary from the request serializer as follows:

    manager.requestSerializer.HTTPRequestHeaders
    

    Note that once you set a header this way, it will be used for all other operations!

提交回复
热议问题