0

I'm sending JSON (initially stored in an NSDictionary) using AFNetworking. My object looks like this (taken from a doc comment):

/**
 *  Sends a create request to the API server
 *  Success will be a dictionary containing:
 *
 *   playlistSession: {
 *       "mediaSegments": {},
 *       "mediaSequence": 0,
 *       "timeElapsed": 0,
 *       "config": {
 *           "maxSegments": 4,
 *           "targetDuration": 10
 *       },
 *       "meta": {
 *           "id": "test",
 *           "shouldBeAvailable": false,
 *           "isAvailable": false,
 *           "shouldFinish": false,
 *           "isFinished": false
 *       }
 *   }
 *
 *  And should be appended to the sessionData dictionary
 */

and I get this on the server:

{ fileSequence: '3',
  playlistSession: 
   { config: { maxSegments: '4', targetDuration: '10' },
     mediaSequence: '0',
     meta: 
      { id: 'MioeXvdiwB',
        isAvailable: '0',
        isFinished: '0',
        shouldBeAvailable: '0',
        shouldFinish: '0' },
     timeElapsed: '0' } }

With characters and strings where numbers and booleans should be. Am I doing something wrong?

Here's the request (the object is stored in an NSMutableDictionary):

self.sessionData[fileSequenceKey] = [NSNumber numberWithInt:fileNumber];
self.sessionData[playlistSessionKey][metaKey][shouldFinishKey] = [NSNumber numberWithBool:lastSegment];

NSString *urlString = [[NSURL URLWithString:[NSString stringWithFormat:kAppendPath, self.postPath] relativeToURL:self.manager.baseURL] absoluteString];

NSURLRequest *request = [self.manager.requestSerializer multipartFormRequestWithMethod:@"POST"
                                                                                     URLString:urlString
                                                                                    parameters:self.sessionData
                                                                     constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                                                                         NSError *error;
                                                                         [formData appendPartWithFileURL:target name:mediaSegmentKey error:&error];
                                                                     }];

        AFHTTPRequestOperation *operation = [self.manager HTTPRequestOperationWithRequest:request
                                                                                  success:[self successBlock:lastSegment]
                                                                                  failure:[self failureBlock:lastSegment]];
        [operation setUploadProgressBlock:[self completionBlock]];

        [self.manager.operationQueue addOperation:operation];
        fileNumber++;
HighFlyingFantasy
  • 3,789
  • 2
  • 26
  • 38

1 Answers1

2

I ended up adding a method to my NSDictionary+JSON category called JSONString

/**
 *  Serializes a JSON string to be sent over the network
 *
 *  @return The serialized playlist session JSON string
 */
- (NSString*)JSONString {
    NSError *error;
    NSData *serializedData = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];

    NSString *JSONString = [[NSString alloc] initWithData:serializedData encoding:NSUTF8StringEncoding];

    return JSONString;
}

returns a NSString that doesn't need to be coerced or parsed, just a simple call to JSON.parse() in nodejs

HighFlyingFantasy
  • 3,789
  • 2
  • 26
  • 38
  • Might want to replace NSJSONWritingPrettyPrinted with 0, but it's personal preference. – JeffRegan Dec 04 '13 at 19:45
  • You're missing out on some AFNetworking functionality that might be helpful down the road. You can just switch from AFHTTPRequestOperation to AFJSONRequestOperation and get this built-in. – Aaron Brager Dec 04 '13 at 19:48
  • I have it on AFJSONRequestOperation, and what is output is unusable without parsing – HighFlyingFantasy Dec 04 '13 at 19:55
  • I too am seeing this issue. Using AFJSONRequestOperation doesn't result in unquoted numbers on the server. @HighFlyingFantasy, it seems like you've made a work-around, which requires tweaking server-side, as opposed to resolving it on the client. Is that right? – Chris Prince May 25 '16 at 13:36
  • I ended up having to make both client-side and server-side changes. See https://stackoverflow.com/questions/37449472/afnetworking-v3-1-0-multipartformrequestwithmethod-uploads-json-numeric-values-w – Chris Prince Jun 07 '16 at 18:19