0

I'm trying to do a POST request to https://gateway.watsonplatform.net/personality-insights/api/v2/profile with application/json as Content-Type and the next json as body:

 {
   "contentItems" : [
     {
       "sourceid" : "twitter",
       "id" : "MYID",
       "userid" : "json",
       "language" : "en",
       "content" : "Call me Ishmael Some years ago-never mind how long precisely-having little or no money in my purse and nothing particular to interest me on shore I thought I would sail about a little and see the watery part of the world It is a way I have of driving off the spleen and regulating the circulation Whenever I find myself growing grim about the mouth whenever it is a damp drizzly November in my soul whenever I find myself involuntarily pausing before coffin warehouses and bringing up the rear of every funeral I meet and especially whenever my hypos get such an upper hand of me that it requires a strong moral principle to prevent me from deliberately stepping into the street and methodically knocking peoples hats off-then I account it high time to get to sea as soon as I can",
       "contenttype" : "text\/plain"
     }
   ]
 }

I'm tried to do this with AFNetworking and the this is as far as I got:

NSString *authStr = [NSString stringWithFormat:@"%@:%@", @"PI-USERNAME", @"PI-PASSWORD"];
 NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
 NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64Encoding]];

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
 [manager.requestSerializer setValue:authValue forHTTPHeaderField:@"Authorization"];

 NSString *sContent = @"Call me Ishmael Some years ago-never mind how long precisely-having little or no money in my purse and nothing particular to interest me on shore I thought I would sail about a little and see the watery part of the world It is a way I have of driving off the spleen and regulating the circulation Whenever I find myself growing grim about the mouth whenever it is a damp drizzly November in my soul whenever I find myself involuntarily pausing before coffin warehouses and bringing up the rear of every funeral I meet and especially whenever my hypos get such an upper hand of me that it requires a strong moral principle to prevent me from deliberately stepping into the street and methodically knocking peoples hats off-then I account it high time to get to sea as soon as I can";

 NSDictionary *myDictionary = [[NSDictionary alloc]initWithObjectsAndKeys:
                                    @"MYID", @"id",
                                    @"jason", @"userid",
                                    @"twitter", @"sourceid",
                                    @"text/plain", @"contenttype",
                                    @"en", @"language",
                                    sContent, @"content",
                                    nil];
 NSArray *myArray = [[NSArray alloc]initWithObjects:myDictionary, nil];
 NSDictionary *parameters = [[NSDictionary alloc]initWithObjectsAndKeys:myArray,@"contentItems", nil];

 [manager POST:@"https://gateway.watsonplatform.net/personality-insights/api/v2/profile" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
     NSLog(@"JSON: %@", responseObject);
 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"Error: %@", error);
 }];

I think it is not using the correct serializer to encode my NSDictionary into a json string

German Attanasio
  • 22,217
  • 7
  • 47
  • 63
  • You can create `parameters` much more easily with the literal syntax. See [New Objective-C Literal Syntax for NSArray, NSDictionary](http://joris.kluivers.nl/blog/2012/03/13/new-objectivec-literal-syntax/) where "New" was three years ago. – zaph Apr 14 '15 at 13:58
  • Thanks for the comment @Zaph! It's been a while since I don't do something in ios – German Attanasio Apr 15 '15 at 18:55

1 Answers1

2

You need to set the correct AFHTTPRequestSerializer for your requested Content-Type before making the request. You can do this as follows:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
aldelucca
  • 36
  • 1