What's wrong with my server side Node Script


#1

hi all
i’ve setup a service (custom scripting service) that do some operation on my mongodb

I want to run this script on a daily basis

so i need to cron a script that will do a POST operation to this Service
but to do a POST i need a Session Token

I’ve setup a nodejs script that will POST a user session in order to get a session token (and after when it will work to a Post to my scripting service)
so,
here is my node js script

var request = require(“request”);

var options = {
dataType: ‘json’,
contentType: ‘application/json; charset=utf-8’,
url : ‘http://Myip/api/v2/user/session’,
method: ‘POST’,
data : JSON.stringify({
‘email’: 'email@email.com,
‘password’: ‘mypassword’
}),
};

request(options, function (error, response, body) {
if(error) {

            console.log(error);
        } else {
            console.log(response.statusCode, body);
        }

});

but i get an error like

400 '{“error”:{“context”:null,“message”:“Login request is missing required email.”,“code”:400,“trace”

any idea on what’s wrong with my script
any other way to POST to my custom scripting service ?

thanks


#2

solved
here is the NODEJS script to login

var request = require(‘request’);

var data = {“email”:“email@email”,“password”:“mypassword”,“duration”: 0}
// var data = { “resource”:[ data ]}
// console.log(data)
var data2 = JSON.stringify(data)

var options = {
dataType: ‘json’,
contentType: ‘application/json; charset=utf-8’,
url : ‘http://Myipapi/v2/user/session’,

form:data2,
method:‘POST’,
headers: { }

};

console.log(options)

request.post(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(" 200")

 console.log(body);
} else {

console.log(“else”)
console.log(response.statusCod);
console.log(body);
}
});

so issue is closed; :slight_smile: