0

I am new to Node Js and trying to make a rest call from Node Js.

I am basically trying to convert a front end Ajax call into a back-end NodeJs call.

Here is the Ajax.

$.ajax({url: 'https://192.168.0.14:3000/printer',
                type: 'post', 
                data:'^XA^FO50,50^GFA,1518,1518,23,L03IF,K03FC1FF,3FFJ01NF^FS^XZ',
                success: function(d) {
                    alert(d);
                }
      });
}

Unfortunately I am stuck without the ability to send Data field. I can't change the other end of API as it is a firmware in a printer.

Here is my failed backend code so far:

I am new to Node Js

    var options = {
        body: '^XA^FO50,50^GFA,1518,1518,23,L03IF,K03FC1FF,K0F8I07C,J03CK0F,J0FCK0FC,I03FCK0FF,I07FCK07F8,I0FF8K07FC,001EF8K078E,003878K0703,0060380IF07018,00C6383KF00C,019E301KF006,033CI0KF803,033CI0KF881,0678I07JFC718,0E7J07IFE038C,0CEJ07IFC018C,1CK07IF8100E,1CK07IF1380E^FS^XZ',
        host: '192.168.0.12',
        path: '/pstprnt',
        method: 'POST',
        json: false,
        data: '^XA^FO50,50^GFA,1518,1518,23,L03IF,K03FC1FF,K0F8I07C,J03CK0F,J0FCK0FC,I03FCK0FF,I07FCK07F8,^FS^XZ',
        formData: '^XA^FO50,50^GFA,1518,1518,23,L03IF,K03FC1FF,K0F8I07C,J03CK0F,J0FCK0FC,I03FCK0FF,I07FCK07F8,I0FF8K07FC,^FS^XZ',


    };

    http.request(options, function (res) {
        console.log('STATUS: ' + res.statusCode);
        console.log('HEADERS: ' + JSON.stringify(res.headers));
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            console.log('BODY: ' + chunk);
        });
    }).end();
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • "I am basically trying to convert a front end Ajax call into a back-end NodeJs call." In that case you should edit your question to include the back-end code you've written so far. – Jordan Running Aug 13 '18 at 15:38
  • @JordanRunning I have made the changes to the Question as requested, Sorry I am quite new to asking question on Online forum. – Raghuram kamath Aug 13 '18 at 15:44
  • @ChristianFritz No, Its actually the get on top level method, The inner level code does a Post. Look at the options field. The outer level get Exposed api is consuming a Post in the Inner level. I have made the changes to the query now.Take a look. Thanks for the help. – Raghuram kamath Aug 13 '18 at 15:48
  • Possible duplicate of [How to make an HTTP POST request in node.js?](https://stackoverflow.com/questions/6158933/how-to-make-an-http-post-request-in-node-js) – Jordan Running Aug 13 '18 at 15:56
  • The options you can pass to `http.request` are [documented](https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_http_request_options_callback). It does not take options named `data`, `body`, or `formData`. To send POST data you must supply it to the `write` or `end` methods of the `http.ClientRequest` object returned by `http.request`. The above linked answer shows how. – Jordan Running Aug 13 '18 at 15:58
  • @JordanRunning Thanks, I actually went through those documents, but did not understand them quite well. I even tried it using Write method. but It did not help much. Anyway thanks for the help. I will keep exploring – Raghuram kamath Aug 13 '18 at 16:03

0 Answers0