• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

146
Vistas
How to add Authorization to node.js POST request

I am setting POST request

var request = {
        host: endpoint,
        method: 'POST',
        path: '/_bulk',
        body: body,
        headers: {
            'Content-Type': 'application/json',
            'Host': endpoint,
            'Content-Length': Buffer.byteLength(body),
            'X-Amz-Security-Token': process.env.AWS_SESSION_TOKEN,
            'X-Amz-Date': datetime,
        }
    };

I need to add Authorization with username and password, what is the right way and syntax to do that ?

over 3 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

For this, you can use auth key or can create a basic token from the username, and password, and pass that token in the header.

const username = 'username';
const password = 'password';

var request = {
    host: endpoint,
    method: 'POST',
    path: '/_bulk',
    body: body,
    headers: {
        'Content-Type': 'application/json',
        'Host': endpoint,
        'Content-Length': Buffer.byteLength(body),
        'X-Amz-Security-Token': process.env.AWS_SESSION_TOKEN,
        'X-Amz-Date': datetime,
    },
    auth: {
        'username': username,
        'password': password
    }
};

Or you can create a basic token from username and password. And put that token in the header against the Authorization key.

const username = 'username'; 
const password = 'password'; 
const encodedBase64Token = Buffer.from(`${username}:${password}`).toString('base64'); 
const authorization = `Basic ${encodedBase64Token}`;
var request = {
    host: endpoint,
    method: 'POST',
    path: '/_bulk',
    body: body,
    headers: {
        'Content-Type': 'application/json',
        'Host': endpoint,
        'Content-Length': Buffer.byteLength(body),
        'X-Amz-Security-Token': process.env.AWS_SESSION_TOKEN,
        'X-Amz-Date': datetime,
        'Authorization': authorization,
    }
};
over 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda