在 Node.JS 上使用请求模块 POST 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6432693/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:18:22  来源:igfitidea点击:

POST data with request module on Node.JS

node.jsrequest

提问by Diego Torres

This module is 'request https://github.com/mikeal/request

这个模块是'请求https://github.com/mikeal/request

I think i'm following every step but i'm missing an argument..

我想我正在遵循每一步,但我错过了一个论点..

var request = require('request');
request.post({
        url: 'http://localhost/test2.php',
         body: "mes=heydude"
         }, function(error, response, body){
            console.log(body);
    });

on the other end i have

在另一端我有

echo $_POST['mes'];

And i know the php isn't wrong...

而且我知道 php 没有错...

回答by Diego Torres

EDIT:You should check out Needle. It does this for you and supports multipart data, and a lot more.

编辑:您应该查看Needle。它为您执行此操作并支持多部分数据等等。

I figured out I was missing a header

我发现我错过了一个标题

var request = require('request');
request.post({
  headers: {'content-type' : 'application/x-www-form-urlencoded'},
  url:     'http://localhost/test2.php',
  body:    "mes=heydude"
}, function(error, response, body){
  console.log(body);
});

回答by TinyTimZamboni

When using requestfor an http POST you can add parameters this way:

当使用request一个HTTP POST,您可以添加参数是这样的:

var request = require('request');
request.post({
  url:     'http://localhost/test2.php',
  form:    { mes: "heydude" }
}, function(error, response, body){
  console.log(body);
});

回答by Raptor

I had to post key value pairs without form and I could do it easily like below:

我不得不发布没有形式的键值对,我可以像下面一样轻松完成:

var request = require('request');

request({
  url: 'http://localhost/test2.php',
  method: 'POST',
  json: {mes: 'heydude'}
}, function(error, response, body){
  console.log(body);
});

回答by Ricky

If you're posting a json body, dont use the formparameter. Using formwill make the arrays into field[0].attribute, field[1].attributeetc. Instead use bodylike so.

如果您要发布 json 正文,请不要使用该form参数。Usingform将使数组变成field[0].attribute, field[1].attributeetc. 而不是body像这样使用。

var jsonDataObj = {'mes': 'hey dude', 'yo': ['im here', 'and here']};
request.post({
    url: 'https://api.site.com',
    body: jsonDataObj,
    json: true
  }, function(error, response, body){
  console.log(body);
});

回答by aposto

var request = require('request');
request.post('http://localhost/test2.php', 
    {form:{ mes: "heydude" }}, 
    function(error, response, body){
        console.log(body);
});

回答by Aniket B

  1. Install request module, using npm install request

  2. In code:

    var request = require('request');
    var data = '{ "request" : "msg", "data:" {"key1":' + Var1 + ', "key2":' + Var2 + '}}';
    var json_obj = JSON.parse(data);
    request.post({
        headers: {'content-type': 'application/json'},
        url: 'http://localhost/PhpPage.php',
        form: json_obj
    }, function(error, response, body){
      console.log(body)
    });
    
  1. 安装请求模块,使用 npm install request

  2. 在代码中:

    var request = require('request');
    var data = '{ "request" : "msg", "data:" {"key1":' + Var1 + ', "key2":' + Var2 + '}}';
    var json_obj = JSON.parse(data);
    request.post({
        headers: {'content-type': 'application/json'},
        url: 'http://localhost/PhpPage.php',
        form: json_obj
    }, function(error, response, body){
      console.log(body)
    });
    

回答by Rahmat Ali

I have to get the data from a POSTmethod of the PHPcode. What worked for me was:

我必须从代码的POST方法中获取数据PHP。对我有用的是:

const querystring = require('querystring');
const request = require('request');

const link = 'http://your-website-link.com/sample.php';
let params = { 'A': 'a', 'B': 'b' };

params = querystring.stringify(params); // changing into querystring eg 'A=a&B=b'

request.post({
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, // important to interect with PHP
  url: link,
  body: params,
}, function(error, response, body){
  console.log(body);
});

回答by Oscar Perez

I highly recommend axios https://www.npmjs.com/package/axiosinstall it with npm or yarn

我强烈推荐 axios https://www.npmjs.com/package/axios用 npm 或 yarn 安装它

const axios = require('axios');

axios.get('http://your_server/your_script.php')
    .then( response => {
    console.log('Respuesta', response.data);
    })
    .catch( response => {
        console.log('Error', response);
    })
    .finally( () => {
        console.log('Finalmente...');
    });