Javascript Axios Post方法实现自定义标头和令牌值
我正试图用NodeJS中的Axios编写一个Post方法 在post方法中,我将以下内容作为参数传递Javascript Axios Post方法实现自定义标头和令牌值,javascript,node.js,axios,axios-mock-adapter,Javascript,Node.js,Axios,Axios Mock Adapter,我正试图用NodeJS中的Axios编写一个Post方法 在post方法中,我将以下内容作为参数传递 url = http:/xyz/oauthToken header 'authorization: Basic kdbvkjhdkhdskhjkjkv='\ header 'cache-control:no-cache' header 'content-type: application/x-www-form-urlencoded' data 'grant_type=password&u
url = http:/xyz/oauthToken
header 'authorization: Basic kdbvkjhdkhdskhjkjkv='\
header 'cache-control:no-cache'
header 'content-type: application/x-www-form-urlencoded'
data 'grant_type=password&username=user123&password=password123'
正如我尝试使用以下代码,但Axioz新手不确定如何准确地实现带有主体响应的grant类型的头
var config = {
headers: {'Authorization': "bearer " + token}
};
var bodyParameters = {
data 'grant_type=password&username=user123&password=password123'
}
Axios.post(
'http:/xyz/oauthToken',
bodyParameters,
config
).then((response) => {
console.log(response)
}).catch((error) => {
console.log(error)
});
任何帮助/建议都将不胜感激:-)目前,axios不方便使用表单编码数据;它主要针对JSON进行了优化。不过,这是可能的
目前,axios不方便使用表单编码数据;它主要针对JSON进行了优化。不过,这是可能的
当我尝试使用“授权”时:
Basic${fjdhjdbkvhowejkbfoilhu=}
`,它向我显示了未终止的字符串自由符错误。您有一个语法错误,听起来像。您是否使用反勾号来打开和关闭字符串?当我尝试使用“授权”时:Basic${fjdhjdbkvhowejkbfoirhu='}
`,它向我显示了未终止的字符串自由体错误。您有语法错误,听起来像是。您是否使用反勾号来打开和关闭字符串?
const querystring = require('querystring');
const body = querystring.stringify({
grant_type: 'password',
username: 'user123',
password: 'password123'
});
axios.post('http:/xyz/oauthToken', body, {
headers: {
authorization: `bearer ${token}`,
'content-type': 'application/x-www-form-urlencoded'
}
});