Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When sending a POST request, when should I JSON.stringify() it or not?

While looking at tutorials and code examples from different places, I noticed that sometimes in a code the JSON object sent through the "data" header in a HTTP request is processed with JSON.stringify(data) before being sent, and sometimes they send it without being "stringified" first.

Using examples from .NET WEB API tutorials, client-side code:

var data = {
    Email: self.registerEmail(),
    Password: self.registerPassword(),
    ConfirmPassword: self.registerPassword2()
};

$.ajax({
    type: 'POST',
    url: '/api/Account/Register',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify(data)
}).done(function (data) {
    self.result("Done!");
}).fail(showError);

In this example, if I understand correctly this will convert data into something like: {"Email":"[email protected]","Password":"Password1!","ConfirmPassword":"Password1!"}

But in this part from the same client-side code:

var loginData = {
    grant_type: 'password',
    username: self.loginEmail(),
    password: self.loginPassword()
};

$.ajax({
    type: 'POST',
    url: '/Token',
    data: loginData
}).done(function (data) {
    self.user(data.userName);
    // Cache the access token in session storage.
    sessionStorage.setItem(tokenKey, data.access_token);
}).fail(showError);

JSON.stringify() isn't used. What would be the difference? If I'm understanding correctly, HTTP requests are text-based and therefore I used to stringify all my requests.

EDIT:

These are the data-bound elements with KnockoutJS:

//...Other HTML markups...

<input class="form-control" type="text" data-bind="value: registerEmail"/>
//...Other HTML markups...

<input class="form-control" type="password" data-bind="value: registerPassword"/>
//...Other HTML markups...

<input class="form-control" type="password" data-bind="value: registerPassword2" />
//...Other HTML markups...

<input class="form-control" type="text" data-bind="value: loginEmail"/>
//...Other HTML markups...

<input class="form-control" type="password" data-bind="value: loginPassword"/>
like image 306
CodeIntern Avatar asked Nov 11 '17 16:11

CodeIntern


People also ask

When should I use JSON Stringify?

The JSON. stringify() method in Javascript is used to create a JSON string out of it. While developing an application using JavaScript, many times it is needed to serialize the data to strings for storing the data into a database or for sending the data to an API or web server.

What is a JSON Stringify () used for?

The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

Can we send JSON object in post request?

To post JSON data to the server, we need to use the HTTP POST request method and set the correct MIME type for the body. The correct MIME type for JSON is application/json. In this POST JSON example, the Content-Type: application/json request header specifies the media type for the resource in the body.


1 Answers

If the object you are sending through the post request doesn't have nested Objects it's safe to send data without converting it to string.

Otherwise, if your payload (data) is something like this:

var data={
  Email:"username",
  Password:"password",
  extraOptions:{
    option1: { Verified:true },
    option2: { empty:false } 
  }
};

Then "JSON.stringify()" it. Or your request body JSON will look like this.

{ 
  Email:"username",
  Password:"password",
  extraOptions[option1][Verified]:true,
  extraOptions[option2][empty]:false
} 

And send data as:

$.ajax({
    type: 'POST',
    url: '/api/Account/Register',
    contentType: 'application/json; charset=utf-8',
    data: {data:JSON.stringify(data)}
}).done(function (data) {
    self.result("Done!");
}).fail(showError);

So in your backend "req.body.data" is your string.

var json=JSON.parse(req.body.data)
like image 87
Bruno Avatar answered Sep 20 '22 03:09

Bruno