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

9
  • it happening because in name attribute of your textfield in html you put 'user[username]' ... Commented Apr 22, 2015 at 7:31
  • 1
    How is this request translated? That's a very strange form to translate the first request into. Commented Apr 22, 2015 at 7:32
  • @MurtazaKhursheedHussain: this happens even when I try using REST client. Commented Apr 22, 2015 at 7:33
  • @EvanKnowles I just tried to print the request body in the server: console.log(JSON.stringify(req.body,null," ")); Commented Apr 22, 2015 at 7:33
  • 3
    How exactly are you sending the data? Are you setting the datatype to JSON? Can you post the code you're using to send the data to the server? Commented Apr 22, 2015 at 7:33

2 Answers 2

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); 
});
Sign up to request clarification or add additional context in comments.

1 Comment

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
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.