프로그래밍/React
React Native REST API에서 POST시 헤더 Authorization Type Basic Auth로 전송해야할 때
sidongmen
2020. 8. 11. 21:03
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);
})