0

I'm new to JS. And have a basic silly doubt. Please bear with me.I want to send a request of the form:

{"user":{"username":"myuser","password":"mypass","role":"myrole"}, "organization":"org_name"}

such that the user object can be access by req.body.user and organisation can be accessed by req.body.organization.

But when I'm sending this request:

it translates to-

{
 "user[username]": "myuser",
 "user[password]": "mypass",
 "user[role]": "myrole",
 "organization": "org_name"
}

When I just send

{"user":{"username":"myuser","password":"mypass","role":"myrole"}}

then I can access using req.body.user, but not the way mentioned above. Why is this so?

How can I send the request now such that I can access the request.body.user and req.body.organization properly?

EDIT:

This is how the request is sent. Front end: ember, backend node/express:

Ember.$.post("http://"+window.location.hostname+":3000/api/organizations/customer",{"user":{"username":"myuser","password":"mypass","role":"myrole"}, "organization":"org_name"},function(data){ 
            console.log(JSON.stringify(data),null, " "); 
        });

Receiving side:

console.log(JSON.stringify(req.body,null," "));

I am trying to create the user but req.body.user is undefined. Though I can use user[username] and proceed, but that s now how I want to do

inquisitive
  • 3,738
  • 6
  • 30
  • 56

2 Answers2

3

You aren't sending JSON to the server. Passing an object in as data doesn't send it as an object; it simply gets converted to a query string [source]. If you want to send JSON, you need to use JSON.stringify on the data that you send, not the data you receive.

Ember.$.post("http://"+window.location.hostname+":3000/api/organizations/customer",JSON.stringify({"user":{"username":"myuser","password":"mypass","role":"myrole"}, "organization":"org_name"}),function(data){ 
  console.log(data); 
});
Christian
  • 19,605
  • 3
  • 54
  • 70
  • It does not work properly. It has stringified in a different way. When I print `console.log(req.body);` this is what i get on the server side : `{ '{"user":{"username":"myuser","password":"mypass","role":"myrole"},"organization":"org_name"}': ' ' }` it seems it has considered whole string as the key and then `'` `'` as the value and still the request.body.user is undefined – inquisitive Apr 22 '15 at 07:49
0

With the help of Christian Varga's answer to this post, I could further dive in. Though the problem was still not solved, but it gave some insight of how can that be solved.

This is what I did next for such a structure. As per his suggestion, I used JSON.stringify but at a different place:

Ember.$.post("http://"+window.location.hostname+":3000/api/organizations/customer",{"user":JSON.stringify({"username":"myuser","password":"mypass","role":"myrole"}), "organization":"org_name"},function(data){ 
  console.log(data); 
});

On the backend server side I could then receive req.body.user. But the req.body.user was stringified JSON. To further access this inner JSON, I had to use JSON.parse.

Though this worked, but I was looking for more better solution, since this backend change worked for my front end implementation, but was failing for RESTClient.

I understood I need to pass the contentType of the data to be sent in the post request. I could not find $.post argument for contentType. So then I used $.ajax call.

Ember.$.ajax({
               url: "http://"+window.location.hostname+":3000/api/organizations/customer",
               type:"POST",
               data: JSON.stringify({user:{username:username,password:password,role:role}, organization_id:organization_id}),
               contentType: "application/json; charset=utf-8",
               dataType:"json",
               success: function(data){ 
            console.log(JSON.stringify(data),null, " "); 
                 }
        });

where the username, password, role, organization_id are already assigned variables.

inquisitive
  • 3,738
  • 6
  • 30
  • 56