Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking 2.0 custom header for POST

I am trying to perform POST request with custom header but I keep getting this exception and app failure:

'NSUnknownKeyException', reason: '[<AFHTTPRequestSerializer 0xa21af80> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key X-Auth-AID.'

I have followed instructions on forum of the code on GitHub:

Both this

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];
[requestSerializer setValue:@"test" forKey:@"X-Auth-AID"];
manager.requestSerializer = requestSerializer;

and this:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:@"test" forKey:@"X-Auth-AID"];

is throwing the exception...

like image 686
Michal Avatar asked May 01 '26 10:05

Michal


1 Answers

The method you want to use is setValue:forHTTPHeaderField:, which is defined on AFHTTPRequestSerializer. The method you're currently calling is setValue:forKey:, which is defined in the the NSKeyValueCoding protocol and not what you want.

like image 123
Edward Dale Avatar answered May 05 '26 07:05

Edward Dale



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!