在Node.js中使用axios发布表单数据 [英] Post form data with axios in Node.js

查看:78
本文介绍了在Node.js中使用axios发布表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Postman上测试Uber API,并且能够成功发送带有表单数据的请求.当我尝试使用Node.js和axios库转换此请求时,出现错误.

I'm testing out the Uber API on Postman, and I'm able to send a request with form data successfully. When I try to translate this request using Node.js and the axios library I get an error.

这是我的邮递员要求的样子:

Here is what my Postman request looks like:

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

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

Here is what I'm doing in Node.js and 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的答复.

When I do this, I get a 400 response.

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

I added the 'multipart/form-data' header because I filled out the form-data in the Postman request. Without the header I get the same result.

我期望得到与Postman相同的响应,Node.js脚本中的config变量是否有问题?

I'm expecting to get the same response I'm getting from Postman, is there something wrong with my config variable in the Node.js script?

任何帮助将不胜感激!

推荐答案

您也许可以使用 Content-Type:'application/x-www-form-urlencoded'.我遇到了与 https://login.microsoftonline.com 相似的问题,该问题无法处理传入的 application/json .

You might be able to use Content-Type: 'application/x-www-form-urlencoded'. I ran into a similar issue with https://login.microsoftonline.com where it was unable to handle incoming application/json.

var axios = require("axios");

axios({
  url: 'https://login.uber.com/oauth/v2/token',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  data: `client_id=${encodeURIComponent('**')}&client_secret=${encodeURIComponent('**')}&grant_type=authorization_code&redirect_uri=${encodeURIComponent('http://localhost:8080/')}&code=${encodeURIComponent('**')}`
})
.then(function(response) {
  console.log(response.data)
})
.catch(function(error) {
  console.log(error)
})

您还可以使用一个函数来像这样处理对formUrlEncoded的翻译

You could also use a function to handle the translation to formUrlEncoded like so

const formUrlEncoded = x =>
   Object.keys(x).reduce((p, c) => p + `&${c}=${encodeURIComponent(x[c])}`, '')

var axios = require("axios");

axios({
  url: 'https://login.uber.com/oauth/v2/token',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  data: formUrlEncoded({
     client_id: '***',
     client_secret: '***',
     grant_type: 'authorization_code',
     redirect_uri: 'http://localhost:8080/',
     code: '***' 
  })
})
.then(function(response) {
  console.log(response.data)
})
.catch(function(error) {
  console.log(error)
})

这篇关于在Node.js中使用axios发布表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆