在Node.js中使用axios发布表单数据

我正在testingPostman上的Uber API,并且能够成功发送带有表单数据的请求。 当我尝试使用Node.js和axios库翻译这个请求时,我得到一个错误。

这是我的邮差请求的样子:

邮递员POST请求

我得到的回应是: { "error": "invalid_client" }

以下是我在Node.js和axios中所做的:

 var axios = require("axios"); const config = { headers: { 'Content-Type': 'multipart/form-data' } }; axios.post('https://login.uber.com/oauth/v2/token', { client_id: '***', client_secret: '***', grant_type: 'authorization_code', redirect_uri: 'http://localhost:8080/', code: '***' }, config) .then(function(response) { console.log(response.data) }) .catch(function(error) { console.log(error) }) 

当我这样做,我得到一个400响应。

我添加了'multipart/form-data'标题,因为我填写了邮递员请求中的表单数据。 没有头我得到相同的结果。

我期望得到我从邮差得到的相同的响应,有没有在我的configurationvariables在Node.js脚本错了吗?

任何帮助,将不胜感激!

至2017年6月10日, axios库不支持在Node.js中发布表单数据。 这是GitHub上的问题 – https://github.com/mzabriskie/axios/issues/789

我们有类似的问题,并决定切换到request库。

从错误看来你的client_id或client_secret是不正确的。 启用debugging并共享原始请求/响应(在过滤凭据后)。