2

I followed the suggested solution at AFNetworking 2.0 add headers to GET request to specify custom headers for the request with the following code snippet:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:someID forHTTPHeaderField:@"some_id"];
NSDictionary *parameters = @{@"id": user.id, @"birthday": user.birthday};
[manager POST:[NSString stringWithFormat:@"%@/user_create",BaseURLString] parameters:parameters
 success:^(AFHTTPRequestOperation *operation, id responseObject)
 {
     if (responseObject[@"error"])
     {
         NSLog(@"REST User Create Response Error: %@", responseObject[@"error"]);
     }
     else
     {
         [self saveUserDetails:responseObject];
     }
 }
 failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
     NSLog(@"REST User Create Error: %@", error);

 }];

But what happens when this gets executed is I get an error in the response from the API stating all my parameters are missing. This same block of code used to work before (without setting the custom header and when the API didn't require them originally).

Does anybody know how to properly set both custom headers and POST parameters?

Thanks, Nino

Community
  • 1
  • 1
Nino
  • 21
  • 3

2 Answers2

2

Your code looks fine to me.

A few things to try:

  1. Try using AFNetworkActivityLogger to see what's actually being sent (set it to AFLoggerLevelDebug.) You can also use a web proxy like Charles or a protocol analyzer like Wireshark.
  2. If you determine the data is not being sent properly, set a breakpoint in [AFURLRequestSerialization -requestBySerializingRequest:withParameters:error:]. This is where your HTTP headers and parameters are added to the URL request. The method is pretty straightforward; you should be able to step through and watch as stuff is added to the request and determine why it gets skipped.

    NOTE: AFURLRequestSerialization.m contains multiple subclasses of AFURLRequestSerialization. Set a breakpoint in the super implementation, as well as in the AFJSONRequestSerializer implementation.

    Examples that could cause this behavior:

    • parameters is nil.
    • you've added POST to HTTPMethodsEncodingParametersInURI but
      • your API is not prepared to handle parameters appended to a URL on a POST request, or
      • the queryStringSerialization block is nil AND queryStringSerializationStyle is set to something other than AFHTTPRequestQueryStringDefaultStyle.
    • NSJSONSerialization can't handle your parameters dictionary

One side note (unrelated to your problem), if you use AFHTTPRequestOperationManager's initWithBaseURL: method, and keep a strong reference to your manager, you won't have to do that [NSString -stringWithFormat:] stuff to construct your URL.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
0

I discovered that I should have set my request serializer to the following instead since my API doesn't require JSON-formatted parameters.

manager.requestSerializer = [AFHTTPRequestSerializer serializer];

Hope this helps others dealing with the same situation as mine.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
Nino
  • 21
  • 3