How is an HTTP POST request made in node.js?

后端 未结 21 2442
南方客
南方客 2020-11-21 23:54

How can I make an outbound HTTP POST request, with data, in node.js?

21条回答
  •  后悔当初
    2020-11-22 00:20

    let request = require('request');
    let jsonObj = {};
    request({
        url: "https://myapii.com/sendJsonData",
        method: "POST",
        json: true,
        body: jsonObj
        }, function (error, resp, body){
           console.log(resp);
    });
    

    Or you could use this library:

    let axios = require("axios");
    let jsonObj = {};
    
    const myJsonAPI = axios.create({
       baseURL: 'https://myapii.com',
       timeout: 120*1000
    });
    
    let response = await myJsonAPI.post("sendJsonData",jsonobj).catch(e=>{
        res.json(e);
    });
    console.log(response);
    

提交回复
热议问题