I am trying to pass two different parameters on the POST Request in Objective-C.
The first parameters should be placed on the Header while the other one should be placed on the Body.
NSDictionary *headerParams = [NSDictionary dictionaryWithObjectsAndKeys:
@"password", @"grant_type",
@"write", @"scope", nil];
NSDictionary *bodyParams = [NSDictionary dictionaryWithObjectsAndKeys:
username, @"username",
password, @"password", nil];
AFHTTPRequestSerializer *r = [AFHTTPRequestSerializer serializer];
NSError *error = nil;
NSData *requestBody = [NSKeyedArchiver archivedDataWithRootObject:bodyParams];
NSMutableURLRequest *request = [r requestWithMethod:@"POST" URLString:urlString parameters:headerParams error:&error];
[request setValue:[NSString stringWithFormat:@"application/json,application/x-www-form-urlencoded"] forHTTPHeaderField:@"Accept"];
[request setValue:[NSString stringWithFormat:@"application/x-www-form-urlencoded"] forHTTPHeaderField:@"ContentType"];
[request setHTTPBody: requestBody]; //--> If I comment this, the headerParams doesn't removed
If I debug the above code with this statement :
po [[NSString alloc] initWithData: request.HTTPBody encoding:4]
I got nil
. But when I omit the [request setHTTPBody: requestBody];
's part, I got the headerParams
's value. I would like to get both headerParams
and bodyParams
's value. What's wrong? Thanks.