1

I wish to perform a POST request from my NodeJS server. It needs to send raw xml data.

What's the recommended approach for this please?

Edit: Adding my current sandbox. Not yet successful!

var express    = require('express');        // call express
var app        = express();                 // define our app using express
var bodyParser = require('body-parser');

// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(bodyParser());

  var data = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:api="http://api.__DOMAIN__.com"><soapenv:Header><api:userToken><password>pwd1</password><username>usr_api</username></api:userToken></soapenv:Header><soapenv:Body><api:SearchModels><!--Optional:--><searchText>keyword</searchText></api:SearchModels></soapenv:Body></soapenv:Envelope>';

  var options = {
     host: 'www.__MY_END_POINT_.com', port:443,
     method: 'POST',
     headers: {
    'Content-Type': 'text/xml',
    'Content-Length': data.length
    }
  };

  var req = http.request(options, function(res)
  {
      res.setEncoding('utf8');
      res.on('data', function (chunk) {
     console.log("body: " + chunk);
   });
  });
  req.write(data);
  req.end();

Thanks, J.

Jem
  • 5,766
  • 12
  • 52
  • 70
  • Surely you have tried a few times. Show your attempts so we can recommend where to improve. – Tomalak Oct 20 '14 at 13:42
  • sure, let me edit the question. – Jem Oct 20 '14 at 13:45
  • @Tomalak just added the code of my last attempt. – Jem Oct 20 '14 at 13:51
  • 1
    First thing that comes to mind: Declare the encoding and don't use `data.length` (but `Buffer.byteLength(data)`). See: http://stackoverflow.com/a/9768327/18771 – Tomalak Oct 20 '14 at 13:53
  • The point is - if your file is UTF-8 (and I think it is), then the number of characters (`data.length`) very likely differs from the number of bytes. – Tomalak Oct 20 '14 at 13:55
  • Hmm seems a good lead, but I feel I'm missing something about the host/port. Postman just takes the https://(...) and properly executes the query. If I give a port 80 in my node.js query, it returns a "301 permanently moved" error. If I give a port 443, it just returns "400 Bad Request" – Jem Oct 20 '14 at 14:28
  • Hmm, it's actually more specific: "The plain HTTP request was sent to HTTPS port" – Jem Oct 20 '14 at 14:33
  • 1
    Aaah, found. I'm using var "http = require('http');" while I should be requiring "var https = require('https');". – Jem Oct 20 '14 at 14:35
  • 1
    "301 permanently moved" is of course not an error, but a redirect, (probably to the same URL, port 443, look at the `Location` header). And you should use `require('https')`, I was just about to say it. – Tomalak Oct 20 '14 at 14:37

1 Answers1

1

Ok, I was wrong about the required module.

Error: var http = require('http');

Good way: var https = require('https');

Jem
  • 5,766
  • 12
  • 52
  • 70
  • 1
    Consider using a convenience wrapper like [needle](https://www.npmjs.org/package/needle) for your HTTP requests, they work around these details. – Tomalak Oct 20 '14 at 14:38
  • Thanks for the hint. Will dig into that! – Jem Oct 20 '14 at 14:43