简体   繁体   English

Node.js:Axios 与使用 x-www-form-urlencoded 获取

[英]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.

相关问题 具有 application/x-www-form-urlencoded 格式的 Node.js Axios POST 请求? - Node.js Axios POST request with application/x-www-form-urlencoded format? 使用application / x-www-form-urlencoded使用node.js在发布请求中发送数组 - Send Array in post request using node.js using application/x-www-form-urlencoded x-www-form-urlencoded 格式 - 在 node.js 中使用 https - x-www-form-urlencoded format - using https in node.js 如何在 node.js 中发布内容类型 ='application/x-www-form-urlencoded' 的数据 - how to post data in node.js with content type ='application/x-www-form-urlencoded' node.js正文解析器对Content-Type:x-www-form-urlencoded和Form-data JSON的错误解释 - node.js body-parser bad interpretation of Content-Type:x-www-form-urlencoded and Form-data JSON 使用 axios 发出 x-www-form-urlencoded 请求 - Making a x-www-form-urlencoded request with axios 如何使用 Axios 在 application/x-www-form-urlencoded 中编码 JSON 数据? - How to encode JSON data in application/x-www-form-urlencoded using Axios? node-fetch 发送 post 请求,正文为 x-www-form-urlencoded - node-fetch send post request with body as x-www-form-urlencoded node js 如何通过发送数据调用webapi( x-www-form-urlencoded ) - node js how to call webapi( x-www-form-urlencoded ) by sending data 如何使用同构提取使用application / x-www-form-urlencoded标头和URLSearchParams进行POST - How to POST with application/x-www-form-urlencoded header and URLSearchParams using isomorphic-fetch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM