[英]How to add data to POST request using Axios?
我正在嘗試發出POST請求,但在chrome網絡標簽中收到以下消息:
{錯誤:“ unsupported_grant_type”,…}錯誤:“ unsupported_grant_type”錯誤說明:“ grant_type必須是client_credentials,authorization_code或refresh_token”
我一直在使用Axios進行RESTful調用,這是POST請求:
async componentDidMount() {
const encodedString = 'blah'//some encoded string
const [initSpotResponse] = await Promise.all([
axios.post('https://accounts.spotify.com/api/token',
{ data: { grant_type: 'client_credentials' } },
{
headers: {
'Authorization': `Basic ${encodedString}`,
'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
}
}
)
]);
}
我已經嘗試了其他StackOverflow帖子中提到的各種方法,但是似乎沒有任何效果。 有沒有人有創建這樣的POST請求的經驗? 我沒有看到有關此問題的特定於axios的帖子-我應該放棄axios(如果是,我應該切換到什么位置)?
axios.post
接受作為第二參數的數據對象( axios.post(url[, data[, config]])
串行化的數據qs
和嘗試此,如上所述在這里 :
const qs = require('querystring'); async componentDidMount() { const encodedString = 'blah'//some encoded string const [initSpotResponse] = await Promise.all([ axios.post('https://accounts.spotify.com/api/token', qs.stringify({ grant_type: 'client_credentials' }), { headers: { 'Authorization': `Basic ${encodedString}`, 'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8' } } ) ]); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.