How to set content-type in AFnetworking

crox picture crox · Jul 21, 2014 · Viewed 19.2k times · Source

I'm having problem with AFNetworking. I can't set content-type to application/json. How can i do? i try a lot of solution, but no one has worked. here your are my code:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
  if (self.enableBasicAuth){
    [manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
    [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:[[SOWMerchant merchant]     username] password:[[SOWMerchant merchant] password]];
  }
  [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  manager.responseSerializer = [AFJSONResponseSerializer serializer];
  [manager POST:[NSString stringWithFormat:@"%@", url] parameters:postData success:^(AFHTTPRequestOperation *operation, id responseObject) {
    [self onSuccessResponse:responseObject request:operation];
  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    [self onError:error request:operation];
  }];

I get this response

{ URL: https://sbengtest.spotonway.com:9099/v1.0/merchant/check }, response: <NSHTTPURLResponse:  0x10aa20530> { URL: https://sbengtest.spotonway.com:9099/v1.0/merchant/check } { status code: 400,     headers {
Connection = "keep-alive";
"Content-Length" = 192;
"Content-Type" = "text/html";
Date = "Sun, 20 Jul 2014 22:34:59 GMT";
Server = "nginx/1.2.1";
} }>

UPDATE:

i trye to add this

AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];

[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

operationManagerInstance.requestSerializer = requestSerializer;

my app crash

UPDATE 2: it works. i removed this line

[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

Answer

thatzprem picture thatzprem · Jun 19, 2015

This worked for me!

[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];