I am not able to get the node.js axios HTTP PUT request working with Authentication Basic <credentials> header. I did try below two approaches but in both the cases I get authentication failure.
Method #1
const name = '<email>';
const pass = '<pass>';
// Base64 string for credentials
const credentials = `${name}:${pass}`;
const baseCredentials = Buffer.from(credentials, 'base64');
// Payload
const params = {
key: 'value'
}
// Headers
const headers = {
headers: {
'Content-Type': 'application/json',
'Content-Length': JSON.stringify(params).length,
'Authorization': 'Basic '+baseCredentials
}
};
// Put request
const result = await axios.put(hostUrl, params, headers);
// Server responds with `HTTP 400 Bad Request`.
Method #2
const name = '<email>';
const pass = '<pass>';
// Payload
const params = {
key: 'value'
}
// Headers
const headers = {
'Content-Type': 'application/json',
'Content-Length': JSON.stringify(params).length
};
// Put request
const result = await axios({
headers: headers,
method: "put",
url: hostUrl,
auth: {
username: name,
password: pass,
},
data: params
});
// Server responds with `HTTP 400 Bad Request`.
I am not sure if there is issue with sequence of the parameters passed to axios.
Same URL with same credentials works well from the postman.