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

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

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

相关标签:
21条回答
  • 2020-11-22 00:35

    I found a video which explains on how to achieve this: https://www.youtube.com/watch?v=nuw48-u3Yrg

    It uses default "http" module together with "querystring" and "stringbuilder" modules. The application takes two numbers (using two textboxes) from a web page and upon submit, returns sum of those two (along with persisting the values in the textboxes). This is the best example I could find anywhere else.

    var http = require("http");
    var qs = require("querystring");
    var StringBuilder = require("stringbuilder");
    
    var port = 9000;
    
    function getCalcHtml(req, resp, data) {
        var sb = new StringBuilder({ newline: "\r\n" });
        sb.appendLine("<html>");
        sb.appendLine(" <body>");
        sb.appendLine("     <form method='post'>");
        sb.appendLine("         <table>");
        sb.appendLine("             <tr>");
        sb.appendLine("                 <td>Enter First No: </td>");
    
        if (data && data.txtFirstNo) {
            sb.appendLine("                 <td><input type='text' id='txtFirstNo' name='txtFirstNo' value='{0}'/></td>", data.txtFirstNo);
        }
        else {
            sb.appendLine("                 <td><input type='text' id='txtFirstNo' name='txtFirstNo' /></td>");
        }
    
        sb.appendLine("             </tr>");
        sb.appendLine("             <tr>");
        sb.appendLine("                 <td>Enter Second No: </td>");
    
        if (data && data.txtSecondNo) {
            sb.appendLine("                 <td><input type='text' id='txtSecondNo' name='txtSecondNo' value='{0}'/></td>", data.txtSecondNo);
        }
        else {
            sb.appendLine("                 <td><input type='text' id='txtSecondNo' name='txtSecondNo' /></td>");
        }
    
        sb.appendLine("             </tr>");
        sb.appendLine("             <tr>");
        sb.appendLine("                 <td><input type='submit' value='Calculate' /></td>");
        sb.appendLine("             </tr>");
    
        if (data && data.txtFirstNo && data.txtSecondNo) {
            var sum = parseInt(data.txtFirstNo) + parseInt(data.txtSecondNo);
            sb.appendLine("             <tr>");
            sb.appendLine("                 <td>Sum: {0}</td>", sum);
            sb.appendLine("             </tr>");
        }
    
        sb.appendLine("         </table>");
        sb.appendLine("     </form>")
        sb.appendLine(" </body>");
        sb.appendLine("</html>");
        sb.build(function (err, result) {
            resp.write(result);
            resp.end();
        });
    }
    
    function getCalcForm(req, resp, data) {
        resp.writeHead(200, { "Content-Type": "text/html" });
        getCalcHtml(req, resp, data);
    }
    
    function getHome(req, resp) {
        resp.writeHead(200, { "Content-Type": "text/html" });
        resp.write("<html><html><head><title>Home</title></head><body>Want to some calculation? Click <a href='/calc'>here</a></body></html>");
        resp.end();
    }
    
    function get404(req, resp) {
        resp.writeHead(404, "Resource Not Found", { "Content-Type": "text/html" });
        resp.write("<html><html><head><title>404</title></head><body>404: Resource not found. Go to <a href='/'>Home</a></body></html>");
        resp.end();
    }
    
    function get405(req, resp) {
        resp.writeHead(405, "Method not supported", { "Content-Type": "text/html" });
        resp.write("<html><html><head><title>405</title></head><body>405: Method not supported</body></html>");
        resp.end();
    }
    
    http.createServer(function (req, resp) {
        switch (req.method) {
            case "GET":
                if (req.url === "/") {
                    getHome(req, resp);
                }
                else if (req.url === "/calc") {
                    getCalcForm(req, resp);
                }
                else {
                    get404(req, resp);
                }
                break;
            case "POST":
                if (req.url === "/calc") {
                    var reqBody = '';
                    req.on('data', function (data) {
                        reqBody += data;
                        if (reqBody.length > 1e7) { //10MB
                            resp.writeHead(413, 'Request Entity Too Large', { 'Content-Type': 'text/html' });
                            resp.end('<!doctype html><html><head><title>413</title></head><body>413: Request Entity Too Large</body></html>');
                        }
                    });
                    req.on('end', function () {
                        var formData = qs.parse(reqBody);
                        getCalcForm(req, resp, formData);
                    });
                }
                else {
                    get404(req, resp);
                }
                break;
            default:
                get405(req, resp);
                break;
        }
    }).listen(port);
    
    0 讨论(0)
  • 2020-11-22 00:35

    Posting another axios example of an axios.post request that uses additional configuration options and custom headers.

    var postData = {
      email: "test@test.com",
      password: "password"
    };
    
    let axiosConfig = {
      headers: {
          'Content-Type': 'application/json;charset=UTF-8',
          "Access-Control-Allow-Origin": "*",
      }
    };
    
    axios.post('http://<host>:<port>/<path>', postData, axiosConfig)
    .then((res) => {
      console.log("RESPONSE RECEIVED: ", res);
    })
    .catch((err) => {
      console.log("AXIOS ERROR: ", err);
    })

    0 讨论(0)
  • 2020-11-22 00:36

    To Post Rest/JSON Request
    We can simply use request package and save the values we have to send in Json variable.

    First install the require package in your console by npm install request --save

    var request = require('request');
    
        var options={
                    'key':'28',
                    'key1':'value',
                    'key2':'value'
                    }
    
        request({
                 url:"http://dev.api.ean.com/ean-services/rs/hotel/v3/ping?                      
                     minorRev="+options.key+
                     "&cid="+options.key1+
                     "&apiKey="+options.key2,
                 method:"POST",
                 json:true},function(error,response,body){
                         console.log(body)
                   }
        );
    
    0 讨论(0)
提交回复
热议问题