How should I pass json data in the request payload of http post request

后端 未结 3 641
谎友^
谎友^ 2021-02-05 01:05

I wanted to know, how to pass the json request in the payload, for eg: {\'name\' : \'test\', \'value\' : \'test\'}:

var post_data = {};

var post_op         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-02-05 02:01

    Use the request module

    npm install -S request

    var request = require('request')
    
    var postData = {
      name: 'test',
      value: 'test'
    }
    
    var url = 'https://www.example.com'
    var options = {
      method: 'post',
      body: postData,
      json: true,
      url: url
    }
    request(options, function (err, res, body) {
      if (err) {
        console.error('error posting json: ', err)
        throw err
      }
      var headers = res.headers
      var statusCode = res.statusCode
      console.log('headers: ', headers)
      console.log('statusCode: ', statusCode)
      console.log('body: ', body)
    })
    

提交回复
热议问题