[英]Node.js: Axios vs Fetch using x-www-form-urlencoded
I normally use Axios for my API requests, but I'm having an issue hitting an external API with Axios for some reason.我通常使用 Axios 来处理我的 API 请求,但是由于某种原因,我遇到了使用 ZD3E28535C74CCEE29818 访问外部 API 的问题。
Fetch on the other hand works fine with this particular request.另一方面,Fetch 可以很好地处理这个特定的请求。
I'm trying to figure out what I'm doing wrong here.我试图弄清楚我在这里做错了什么。
Axios: Axios:
async function getToken(req, res) { const apiCreds = { 'grant_type': 'client_credentials', 'client_id': xxxxxxxxxxxx, 'client_secret': xxxxxxxxxxxx, 'scope': 'PublicAPI' } const header = { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } } const token = await axios.post(`${process.env.URL}`,qs.stringify(apiCreds),header) res.status(200).json({'TokenData': token}) }
I tried adding qs.stringify
to the object body, but that didn't work either.我尝试将
qs.stringify
添加到 object 主体中,但这也不起作用。
Fetch:拿来:
async function getToken(req, res) { const apiCreds = { 'grant_type': 'client_credentials', 'client_id': xxxxxxxxxxx, 'client_secret': xxxxxxxxxxxxx, 'scope': 'PublicAPI' } fetch(`${process.env.VS_URL}`, { method: 'post', body: qs.stringify(apiCreds), headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, }).then(res => res.json()).then(json => { res.status(200).json({'TokenData': json}) }); }
I really want to know what I'm doing wrong here so I can stick with one package if possible.我真的很想知道我在这里做错了什么,所以如果可能的话,我可以坚持使用一个 package。
Any feedback is appreciated!任何反馈表示赞赏!
This was completely my fault.这完全是我的错。 I didn't realize the API call actually went through and I was getting a JSON circular error!
我没有意识到 API 调用实际上通过了,我收到了 JSON 循环错误! I just needed to format the data from the result of the API call.
我只需要从 API 调用的结果中格式化数据。
res.status(200).json({'TokenData': token.data.access_token})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.