"Can not POST" in node.js and postman

3

It happens that I have this code, where I try to pass by the user parameter and password using as a postman tool, I'm honestly new to this so I do not know if I'm doing well or the code is wrong since I get this error in postman

app.post('api/v1/Login/:user/:pass',function(request, res){


 var usuario = request.params.user;
  var contraseña = request.params.pass;

  var request = new sql.Request();
  try{

  request.query("SELECT * FROM dbo.[Client] WHERE username = '"+UsuarioReg+"'AND pass = '"+ContraReg+"'", function (err, recordset) {
    if(err){
    console.log(err);
    }else{
      if(recordset.rowsAffected > 0){
        res.send(JSON.stringify("Usuario identificado correctamente"))
        res.send(JSON.stringify(recordset))
        console.log("Usuario identificado correctamente")
      }else{
        console.log("Usuario ["+UsuarioReg+"] no existe")
      }
    }
  })
}catch(err){
  res.send(JSON.stringify("Error while querying database :- "+err))
  console.log("Error while querying database :- "+err)
}

});

I would like to know what happens and how to solve it! Sorry if the question is not clear.

    
asked by java005 05.09.2018 в 19:23
source

2 answers

6

My first recommendation is that you ignore the comments of Mauricio Arias and Valvert, you should never send sensitive information by URL and less with a GET method, much less when it comes to users and passwords.

The problem that you are currently presenting is because you send the parameters through the url, which is not correct and that is why it sends you the error, basically it means that you do not find in endpoint that you are requesting from Postman.

To achieve what you need, to send data by a POST method, you can use the module body-parser , its use and implementation are quite simple:

var express = require('express');
var bodyParser = require('body-parser')

var jsonParser = bodyParser.json()

app.post('/login', jsonParser, function (req, res) {
    if (!req.body)
        return res.sendStatus(400)
    console.log(req.body);

    var user = req.body.user;
    res.send('Bienvenido ' + user)
})

To make the call to the endpoint from Postman, it would be as follows:

  
  • URL: link : {port} / login (you should replace {puerto} for the one you currently use in your localhost      
    • Method: POST
    •   
    • Headers:   Content-type: application / json
    •   
    • Body:     
          {
              "user" : "myuser",
              "pass":"mypass"
          }
    •   
  •   

Exit in the console would be:

  

{user: 'myuser', pass: 'mypass'}

The answer in the Postman would be:

  

Welcome myuser

Making the total adjustment, your code would be as follows:

app.post('/login', jsonParser, function (req, res) {
    if (!req.body)
        return res.sendStatus(400)

    var user = req.body.user;
    var pass = req.body.pass;

    var request = new sql.Request();
    try {
        request.query("SELECT * FROM dbo.[Client] WHERE username = '" + user + "'AND pass = '" + pass + "'", function (err, recordset) {
            if (err) {
                console.log(err);
            } else {
                if (recordset.rowsAffected > 0) {
                    res.send(JSON.stringify("Usuario identificado correctamente"))
                    res.send(JSON.stringify(recordset))
                    console.log("Usuario identificado correctamente")
                } else {
                    console.log("Usuario [" + UsuarioReg + "] no existe")
                }
            }
        })
    } catch (err) {
        res.send(JSON.stringify("Error while querying database :- " + err))
        console.log("Error while querying database :- " + err)
    }
})
    
answered by 05.09.2018 / 23:09
source
-1

You could base yourself on this example. For that matter, execute an SP

exports.moduloValidacion = function(req, res)
{
    var post = {}
    sql.connect(config, function (err) {
        if (err)
        {
            console.log("Log Error"+' '+err.originalError);
            res.status(500).send(err.originalError);
        }
        else
        {
            var request = new sql.Request();
            request.input('user', req.body.user);
            request.input('pass', req.body.pass);
            request.execute('SPValidaUsuario', (err, result) => {
                if(err){console.log("Log Error"+err.originalError.info.message);
                    res.status(500).send(err.originalError.info)}
                else
                {
                    if(result)
                    {
                        console.log("Log Ok");
                        res.status(200).send(result.recordsets[0])
                    }
                }
                sql.close()
            })
        }                   
    })
};
    
answered by 05.09.2018 в 23:05