如何与爱可信NPM包权威性
在过去,我用一些PHP代码查询这样的API
$str = file_get_contents('https://bla:[email protected]/rest/api/content/2950446?expand=body.storage');
$jsonObj = json_decode($str, true);
现在,我想用JS节点构建Alexa的技巧
我测试了这个
var session_url = '';
const fetchQuotes = async () => {
try {
const { data } = await axios.post(session_url, {}, {
auth: {
username: 'bla',
password: 'bla'
}});
return data;
} catch (error) {
console.error('cannot fetch quotes', error);
}
};
从我的理解,这也应该这样做。是这样吗?调用该网址,但我得到一个auth错误......
回答如下:你把你的data
在options
领域,当你调用axios.post
。你的数据需要axios.post
的第二个参数不是第三位。
在做axios.post(url, {}, { somethingHere }
您data
等于{}
(空对象)。你应该这样做,而不是axios.post(url, { somethingHere }, { config }
和config
部分是可选的。
Correct argument order
const fetchQuotes = async () => {
try {
const { data } = await axios.post(session_url, {
auth: {
username: 'bla',
password: 'bla'
}});
return data;
} catch (error) {
console.error('cannot fetch quotes', error);
}
};
如何与爱可信NPM包权威性
在过去,我用一些PHP代码查询这样的API
$str = file_get_contents('https://bla:[email protected]/rest/api/content/2950446?expand=body.storage');
$jsonObj = json_decode($str, true);
现在,我想用JS节点构建Alexa的技巧
我测试了这个
var session_url = '';
const fetchQuotes = async () => {
try {
const { data } = await axios.post(session_url, {}, {
auth: {
username: 'bla',
password: 'bla'
}});
return data;
} catch (error) {
console.error('cannot fetch quotes', error);
}
};
从我的理解,这也应该这样做。是这样吗?调用该网址,但我得到一个auth错误......
回答如下:你把你的data
在options
领域,当你调用axios.post
。你的数据需要axios.post
的第二个参数不是第三位。
在做axios.post(url, {}, { somethingHere }
您data
等于{}
(空对象)。你应该这样做,而不是axios.post(url, { somethingHere }, { config }
和config
部分是可选的。
Correct argument order
const fetchQuotes = async () => {
try {
const { data } = await axios.post(session_url, {
auth: {
username: 'bla',
password: 'bla'
}});
return data;
} catch (error) {
console.error('cannot fetch quotes', error);
}
};