Javascript 在 Node.js 中使用 axios 发布表单数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41764184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Post form data with axios in Node.js
提问by Mike
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.
我正在 Postman 上测试 Uber API,并且能够成功发送包含表单数据的请求。当我尝试使用 Node.js 和 axios 库翻译此请求时,出现错误。
Here is what my Postman request looks like:
这是我的 Postman 请求的样子:
The response I get is: { "error": "invalid_client" }
我得到的回应是: { "error": "invalid_client" }
Here is what I'm doing in Node.js and axios:
这是我在 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)
})
When I do this, I get a 400 response.
当我这样做时,我得到 400 响应。
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.
我添加了'multipart/form-data'标题,因为我在 Postman 请求中填写了表单数据。没有标题,我得到相同的结果。
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?
我希望得到与 Postman 相同的响应,我的 Node.js 脚本中的 config 变量有问题吗?
Any help would be appreciated!
任何帮助,将不胜感激!
回答by Reid Evans
You might be able to use Content-Type: 'application/x-www-form-urlencoded'. I ran into a similar issue with https://login.microsoftonline.comwhere it was unable to handle incoming application/json.
您也许可以使用Content-Type: 'application/x-www-form-urlencoded'. 我遇到了类似的问题https://login.microsoftonline.com,它无法处理传入的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)
})
You could also use a function to handle the translation to formUrlEncoded like so
您还可以使用一个函数来处理转换为 formUrlEncoded 像这样
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)
})
回答by anuveyatsu
As for 10 June 2017, axioslibrary does not support posting form data in Node.js. Here is the issue on GitHub - https://github.com/mzabriskie/axios/issues/789
至于 2017 年 6 月 10 日,axios库不支持在 Node.js 中发布表单数据。这是 GitHub 上的问题 - https://github.com/mzabriskie/axios/issues/789
We had the similar problem and decided to switch to requestlibrary.
我们遇到了类似的问题,并决定切换到request图书馆。
回答by Dustin Whittle
From the error it seems your client_id or client_secret is incorrect. Enable debugging and share the raw request/response (after filtering credentials out).
从错误看来,您的 client_id 或 client_secret 不正确。启用调试并共享原始请求/响应(过滤凭据后)。
回答by salihcenap
It is not true! You can post data with axios using nodejs. I have done it. The problem is, if you use PHP on the server side, there is a pitfall you need to be aware of. Axios posts data in JSON format (Content-Type: application/json) PHP's standard $_POST array is not populated when this content type is used. So it will always be empty. In order to get post parameters sent via a json request, you need to use file_get_contents("http://php://input") .
这不是真的!您可以使用 nodejs 使用 axios 发布数据。我已经做了。问题是,如果您在服务器端使用 PHP,则需要注意一个陷阱。Axios 以 JSON 格式发布数据(内容类型:application/json) 使用此内容类型时,不会填充 PHP 的标准 $_POST 数组。所以它永远是空的。为了获取通过 json 请求发送的 post 参数,您需要使用 file_get_contents(" http://php://input") 。
A simple PHP script on the server side would be:
服务器端的一个简单的 PHP 脚本是:
if($_SERVER['REQUEST_METHOD']==='POST' && empty($_POST)) {
$_POST = json_decode(file_get_contents('http://php://input'));
}
Using this method you can avoid the formData dependency. You CANpost data directly from node.js.
使用这种方法可以避免 formData 依赖。您CAN直接从node.js中发布数据


