I'm writing an api that gets a JSON in NODE.JS. The method of sending the data is POST.

To ease the management of requests I'm using lib Express and BodyParser to interpret the body of POST requests.

I'm grouping information into a javascript object like this:

const data = {
    "plan": "312409959F9FDDE444959F9C950201D7",
    "sender": {
        "name": $("[name=pname]").val(),
        "email": $("[name=pemail]").val(),
        "hash": credentials.hash,
        "phone": {
            "areaCode": $("[name=pphone]").val().substring(0, 2),
            "number": $("[name=pphone]").val().substring(2, 11)
        },
        "address": {
            "street": $("[name=paddress]").val(),
            "number": $("[name=pnumber]").val(),
            "complement": $("[name=pcomplement]").val(),
            "district": $("[name=pbairro]").val(),
            "city": $("[name=pcity]").val(),
            "state": $("[name=puf]").val(),
            "country": "BRA",
            "postalCode": $("[name=pcep]").val()
        },
        "documents": [{
            "type": "CPF",
            "value": $("[name=pcpf]").val()
        }]
    },
    "paymentMethod": {
        "type": "CREDITCARD",
        "creditCard": {
            "token": credentials.token,
            "holder": {
                "name": $("[name=pownername]").val(),
                "birthDate": $("[name=pbirthday]").val().split('-').reverse().join('/'),
                "documents": [{
                    "type": "CPF",
                    "value": $("[name=pcpf2]").val()
                }],
                "phone": {
                    "areaCode": $("[name=pphone2]").val().substring(0, 2),
                    "number": $("[name=pphone2]").val().substring(2, 11)
                }
            },
        }
    }

}

The request is sent as follows:

$.post('/adherence', data, function (msg) {
    console.log(msg)
})

And the receipt on my backend is like this:

app.post('/adherence', (req, res) => {
    try {
        console.log(req.body);
        res.send("OK");
    } catch (e) {
        console.log(e);
        res.send(e);
    }
});

But what I get in req.body is this:

{ plan: '312409959F9FDDE444959F9C950201D7',
 'sender[name]': 'FRancisco',
  'sender[email]': '[email protected]',
  'sender[hash]': 'ceedf0fd2ffd35f4054104d305088e19e8ca9333bbf70be10bd2ea6f94af226a',
  'sender[phone][areaCode]': '63',
  'sender[phone][number]': '991047876',
  'sender[address][street]': '1105 sul qi 3 al 3 lt 1',
  'sender[address][number]': '1',
  'sender[address][complement]': '',
  'sender[address][district]': 'centro',
  'sender[address][city]': 'palmas',
  'sender[address][state]': 'TO',
  'sender[address][country]': 'BRA',
  'sender[address][postalCode]': 'CEP',
  'sender[documents][0][type]': 'CPF',
  'sender[documents][0][value]': 'CPF',
  'paymentMethod[type]': 'CREDITCARD',
  'paymentMethod[creditCard][token]': 'b2f303ba63964404b6c466323deb9078',
  'paymentMethod[creditCard][holder][name]': 'LUCINEIA',
  'paymentMethod[creditCard][holder][birthDate]': '11/01/1990',
  'paymentMethod[creditCard][holder][documents][0][type]': 'CPF',
  'paymentMethod[creditCard][holder][documents][0][value]': 'CPF',
  'paymentMethod[creditCard][holder][phone][areaCode]': '63',
  'paymentMethod[creditCard][holder][phone][number]': '991047876' }

In my opinion, everything is normal, and it was for me to be receiving the JSON in the format sent. What's wrong with my code?

1 Answers

1
sensor On Best Solutions

Try sending your request like that:

$.ajax({
    url: '/adherence',
    type: 'POST',
    data: JSON.stringify(data),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (msg) {
        console.log(msg)
    })
});