Michael Choi Michael Choi - 3 years ago 256
Swift Question

Hapi - How to parse the payload to JSON?

So I'm trying to study up on HTTP/TCP/IP Protocols, Nodejs, MongoJS,and MongoDB all at once. So I'm trying to figure out a couple things with the Hapi Web Framework. I am able to communicate with the server with my iOS simulator however, I can't seem to parse the payload right. I understand I could use parameters in the URL to send the information but I would like to use the payload.

So I end up with this being saved into Mongo.

"_id" = 5431f161bb859872034d2456;
"{\"userLastNameKey\":\"Kwon\",\"userEmailKey\":\"email\",\"userFirstNameKey\":\"Michael\",\"userUsernameKey\":\"username\",\"userPasswordKey\":\"password\"}" = "";
"_id" = 5431fe5694ed4721046c1f8c;
"{\"userLastNameKey\":\"Kwon2\",\"userEmailKey\":\"email2\",\"userFirstNameKey\":\"Michael2\",\"userUsernameKey\":\"username2\",\"userPasswordKey\":\"password2\"}" = "";

This is my hapi code for the call.

// This will add a new user to the database
function addUser(request, response){

db.usersCollection.save(request.payload, function (err, saved){
if(err || !saved)
console.log("User not saved");
} else
console.log("User saved");

My end result I would like to try and get it to be like this

"_id" = 5431f161bb859872034d2456,
"userLastNameKey" = "Kwon",
"userEmailKey" = "email",
"userFirstNameKey"= "Michael",
"userUsernameKey" = "username",
"userPasswordKey" = "password",

Here's the iOS code

NSDictionary *userData = [User userToDictionary: newUserInfo];
NSData *userJSON = [NSJSONSerialization dataWithJSONObject: userData options: 0 error: nil];

NSURL *url = [NSURL URLWithString: urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url];
[request setHTTPMethod: @"POST"];
[request addValue: @"application/json" forHTTPHeaderField: @"Content-Type"];

// This will set up the url session
NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration: config];

NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest: request fromData: userJSON completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {


} else



[uploadTask resume];

Anyone know how I can achieve this?

Answer Source
  "_id" = 5431f161bb859872034d2456;
  "userLastNameKey" = "Kwon"; 
  "userEmailKey" = "email";
  "userFirstNameKey"= "Michael";
  "userUsernameKey" = "username";
  "userPasswordKey" = "password";

That is NOT a JSON object. You need to use commas instead of semicolons. and colons instead of equals. Your ID is also not a decimal number so it's probably best to make it a string.

Your object would look like :

  "_id" : "5431f161bb859872034d2456",
  "userLastNameKey" : "Kwon",
  "userEmailKey" : "email",
  "userFirstNameKey" : "Michael",
  "userUsernameKey" : "username",
  "userPasswordKey" : "password"

To parse a (well-formatted) JSON string, use : var JSONObject = JSON.parse(JSONstring);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download