0

I am building an app that sends json in an NSMutableURLRequest to a server where i have Node and Express running.

The JSON payload is getting printed out in node looking like this:

{ '{"firstName":"Scotty","lastName":"Lastname","profileId":"123454321234"}': '' }

My node code looks like this

var http = require("http");
var express = require("express");
var bodyParser = require('body-parser');
var mongoose = require("mongoose");
var remote_db_url = require('./database.js');
mongoose.connect(remote_db_url.url); 

var user = require('./user.model.js');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));

var user_data = new user();

app.post("/account/login", function(req,res)
{
    var user = req.body;
    console.log("user is: "+ user);
    console.log("firstname is: " + user.firstName);

    user_data.save(function(err,suc) {
        if (err)
            res.json({"status" : 400,"error":"Bad request"});
        else
            res.json({"status":200,"id" : suc.id });
    });
});

The console log statements show like this:

user is: { '{"firstName":"Scotty","lastName":"Lastname","profileId":"123454321234"}': '' }
firstname is: undefined

I proxied through charles and can confirm the payload looks like this:

POST /account/login HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Connection: keep-alive
Accept: */*
User-Agent: test/1 CFNetwork/711.1.12 Darwin/14.0.0
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Content-Length: 79
Host: 192.168.1.4:3000

{"firstName":"Scotty","lastName":"Lastname","profileId":"123454321234"}

I cant seem to get the json parsed correctly. Any ideas here?

Scoota P
  • 2,622
  • 7
  • 29
  • 45

1 Answers1

0

I found out the problem here! The problem was that i need to specifically set the content type to

 application/json; charset=UTF-8

Thanks to this post for helping me out:

jQuery ajax, how to send JSON instead of QueryString

Community
  • 1
  • 1
Scoota P
  • 2,622
  • 7
  • 29
  • 45