POST data with request module on Node.JS

后端 未结 8 1387
青春惊慌失措
青春惊慌失措 2020-12-07 09:14

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

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

var request = require(\'         


        
8条回答
  •  無奈伤痛
    2020-12-07 09:56

    I have to get the data from a POST method of the PHP code. What worked for me was:

    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);
    });
    

提交回复
热议问题