npm install base-64

.d.ts 를 지원하므로 typescript로 작성한다면 @types/basic-64를 설치하면 됨.

 

import base64 from 'base-64';

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();

function parseJSON(response) {
return response.json()
}

나의 경우 아래와 같이 작성함

        let data = {
            method: 'POST',
            // body: JSON.stringify({

            // })
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
                'Authorization': 'Basic ' + base64.encode('user:password')
            }
        }

        fetch(`http://url`,data)
        .then((response)=>response.json())
        .then((json)=>{
            console.log(json);
        })

 

 

+ Recent posts