0

So, basically I am trying to make a JSON Request against a WS in which one of its parameter is an Array using AFNetworking 3.0 My Json should look something like:

{
  "filtrosAlerta": [
    {
      "data": "",
      "type": ""
    }
  ],
  "frecuency": 0
}

The backend error doesn't say much really (Request failed: internal server error (500)) but after troubleshooting this in different ways, the Array inside the Dictionary seems to be the problem. So I am sending the following request through AFNetworking 3.0:

 AFHTTPSessionManager        *manager = [AFHTTPSessionManager manager];
AFJSONRequestSerializer     *serializer = [AFJSONRequestSerializer serializer];
manager.requestSerializer = serializer;

[manager.requestSerializer setAuthorizationHeaderFieldWithCredential:credential];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",nil];

NSString *url = @"http://www.example.com/webservice"

NSArray  *filters = @[@{@"data" : @"java", @"type" :@"query"}, @{@"data" : @"29", @"type" :@"provincia"}];        
NSDictionary *parameters = @{@"filtrosAlerta":filters, @"frecuenciaNotif":[NSNumber numberWithInt:0]]

[manager POST:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nullable task, id response) {
    completionBlock(response, nil);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    completionBlock(nil, error);
}];

My problem is that when I add the NSArray inside the NSDictionary I get an error from the backend server. If I try to debug the "parameters" NSDictionary inside xcode I get the following thing:

{filtrosAlerta = ({data = java;type = query;}{data = 29;type = provincia;});
frecuenciaNotif = 7;}

Please note that instead of the normal "[]" Json Array I seem to be getting a "()" array which doesn't seem to be right, but this is really xcode debugger, and I believe that eventually AFNetworking will get each object inside the NSDictionary and create the proper JSON string! I've been looking around and tried different answers but could not get this working yet.

brandonscript
  • 68,675
  • 32
  • 163
  • 220
Ignacio Oroná
  • 4,371
  • 1
  • 19
  • 25
  • It does translate your parameters in JSON, since you set its `requestSerializer` as an `AFJSONRequestSerializer` object. Does it works when you tries with POSTMAN or assimilated? – Larme Jan 27 '17 at 17:11
  • Yes @Larme it works just fine with Postman! – Ignacio Oroná Jan 27 '17 at 17:16
  • What error do you get from AFNetworking? – Sviatoslav Yakymiv Jan 27 '17 at 17:19
  • I don't get an error from AFNetworking actually, but one from the backend server that is trying to run the WS. The error I get is: Request failed: internal server error (500) – Ignacio Oroná Jan 27 '17 at 17:26
  • Try logging your request JSON via http://stackoverflow.com/a/9020923/1214800 instead to see if the output string matches what you're expecting? This could also be a UTF-8 encoding issue. – brandonscript Jan 27 '17 at 17:27
  • Good idea @brandonscript I did that and I get: { "filtrosAlerta" : [ { "type" : "query", "data" : "java" }, { "type" : "provincia", "data" : "29" } ] "frecuenciaNotif" : 7 } Everything looks cool – Ignacio Oroná Jan 27 '17 at 17:32
  • 1
    Now copy and paste that output directly from Xcode into Postman and send it — see what happens – brandonscript Jan 27 '17 at 17:35
  • I would send the json as it is. Convert it to NSData and send it as that. – Alex Jan 27 '17 at 19:33

0 Answers0