I want to do a post request passing some credentials using isomorphic-fetch. Here is the code I have for now:
export default function fetchJson(url: string, options: any) {
options.headers = (<any>Object).assign({
'Accept': 'application/json',
'Content-Type': 'application/json'
}, options.headers);
return fetch(url, options)
.then(checkStatus)
.then(parseJSON);
}
export function fetchLogs(dispatch_ok: any, dispatch_error: any) {
var URL = "http://server_address:8080/api/getlogs/";
return fetchJson(URL, {
method: 'get',
credentials: 'include',
headers: {
Authorization: 'Basic '+window.btoa('user:passwd'),
}
}).then((data: any) => {
if (data.results.length != 0) {
dispatch_ok(data.results);
} else {
dispatch_error("No logs could be retrieved");
}
}).catch((error: any) => {
dispatch_error(error.message);
});
}
This code does not work as it redirects to the login page. Any idea on how to make this work ?
Basic auth with isomorphic-fetch
The code you have will fine. i.e. it will post the authorization header just fine.
The redirecting logic is most likely broken.