Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

808
Views
¿Autenticación básica con fetch?

Quiero escribir una autenticación básica simple con fetch, pero sigo recibiendo un error 401. Sería increíble si alguien me dice qué está mal con el código:

 let base64 = require('base-64'); let url = 'http://eu.httpbin.org/basic-auth/user/passwd'; let username = 'user'; let password = 'passwd'; let headers = new Headers(); //headers.append('Content-Type', 'text/json'); headers.append('Authorization', 'Basic' + base64.encode(username + ":" + password)); fetch(url, {method:'GET', headers: headers, //credentials: 'user:passwd' }) .then(response => response.json()) .then(json => console.log(json)); //.done();
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Le falta un espacio entre Basic y el nombre de usuario y la contraseña codificados.

 headers.set('Authorization', 'Basic ' + base64.encode(username + ":" + password));
over 4 years ago · Santiago Trujillo Report

0

Una solución sin dependencias.

Nodo

 headers.set('Authorization', 'Basic ' + Buffer.from(username + ":" + password).toString('base64'));

Navegador

 headers.set('Authorization', 'Basic ' + btoa(username + ":" + password));
over 4 years ago · Santiago Trujillo Report

0

En JavaScript puro, también puede usar btoa en lugar de base64.encode() :

 headers.set('Authorization', 'Basic ' + btoa(username + ":" + password));

Tenga en cuenta que esto solo funcionará con caracteres ASCII.

Si tiene que manejar diferentes codificaciones, consulte la documentación btoa vinculada.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!