how to send information from javascript to flask route


#python route
@app.route('/this-route', methods=['GET', 'POST']):
def thisRoute():
information = request.data
return "1"

#javascript
const URL = '/get-coordinates'
const xhr = new XMLHttpRequest();
sender = JSON.stringify([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
xhr.open('POST', URL);
xhr.send(sender);

www.codegrepper.com
www.stackoverflow.com

How to send JSON Web Token (JWT Token) as header with Postman and golang


func ProtectedEndpoint(w http.ResponseWriter, req *http.Request) {
params := req.URL.Query()
token, _ := jwt.Parse(params["token"][0], func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("There was an error")
}
return []byte("secret"), nil
})
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
var user User
mapstructure.Decode(claims, &user)
json.NewEncoder(w).Encode(user)
} else {
json.NewEncoder(w).Encode(Exception{Message: "Invalid authorization token"})
}
}

func CreateTokenEndpoint(w http.ResponseWriter, req *http.Request) {
var user User
_ = json.NewDecoder(req.Body).Decode(&user)
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"username": user.Username,
})
tokenString, error := token.SignedString([]byte("secret"))
if error != nil {
fmt.Println(error)
}
json.NewEncoder(w).Encode(JwtToken{Token: tokenString})
}

www.codegrepper.com
www.stackoverflow.com

how to send mail node.js


var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'youremail@gmail.com',
pass: 'yourpassword'
}
});

var mailOptions = {
from: 'youremail@gmail.com',
to: 'myfriend@yahoo.com',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};

transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});

www.codegrepper.com
www.stackoverflow.com

how to send message to specific client using ws node js


var Server = require('ws').Server;
var port = process.env.PORT || 9030;
var ws = new Server({port: port});

var sockets = [];
ws.on('connection', function(w){

var id = w.upgradeReq.headers['sec-websocket-key'];
console.log('New Connection id :: ', id);
w.send(id);
w.on('message', function(msg){
var id = w.upgradeReq.headers['sec-websocket-key'];
var message = JSON.parse(msg);

sockets[message.to].send(message.message);

console.log('Message on :: ', id);
console.log('On message :: ', msg);
});

w.on('close', function() {
var id = w.upgradeReq.headers['sec-websocket-key'];
console.log('Closing :: ', id);
});

sockets[id] = w;
});

www.codegrepper.com
www.stackoverflow.com