How do I properly submit an XML post request in node?

I have written the following code to submit a post request. The server expects the request in XML. I keep getting the response OPERTAION_FAILED with the message Cannot consume content type. I have followed the exact xml format provided in the API documetation. Could you please help me on what I'm doing wrong?

var request = require('request');
var crypto = require('crypto');
var xml = require('xml');
var express = require("express");
var app = express();
var utctime = new Date().toISOString()

var login = [SENSITIVE DATA];
var password = [SENSITIVE DATA];
var requestId = [SENSITIVE DATA];
var ownTaxNumber = [SENSITIVE DATA];
var searchedTaxNumber = [SENSITIVE DATA];
var signingKey = [SENSITIVE DATA];
var softwareId = [SENSITIVE DATA];

var hash = crypto.createHash('sha512');
data = hash.update(password, 'utf-8');
var hashPassword = data.digest('hex').toUpperCase();
var hash = crypto.createHash('sha512');
data = hash.update(requestId + utctime + signingKey, 'utf-8');
var requestSignature = data.digest('hex').toUpperCase();

var reqbody = '';
reqbody = reqbody + '<?xml version="1.0" encoding="UTF-8"?>';
reqbody = reqbody + '<QueryTaxpayerRequest xmlns="http://example.com/OSA/1.0/api">';
reqbody = reqbody + '<header>';
reqbody = reqbody + '<requestId>' + requestId + '</requestId>';
reqbody = reqbody + '<timestamp>' + utctime + '</timestamp>';
reqbody = reqbody + '<requestVersion>1.1</requestVersion>';
reqbody = reqbody + '<headerVersion>1.0</headerVersion>';
reqbody = reqbody + '</header>';
reqbody = reqbody + '<user>';
reqbody = reqbody + '<login>' + login + '</login>';
reqbody = reqbody + '<passwordHash>' + hashPassword + '</passwordHash>';
reqbody = reqbody + '<taxNumber>' + ownTaxNumber + '</taxNumber>';
reqbody = reqbody + '<requestSignature>' + requestSignature + '</requestSignature>';
reqbody = reqbody + '</user>';
reqbody = reqbody + '<software>';
reqbody = reqbody + '<softwareId>' + softwareId + '</softwareId>';
reqbody = reqbody + '</software>';
reqbody = reqbody + '<taxNumber>' + searchedTaxNumber + '</taxNumber>';
reqbody = reqbody + '</QueryTaxpayerRequest>';

console.log(reqbody);

request.post('https://example.com/invoiceService/queryTaxpayer', function (error, response, body) {
    response.headers['content-type'] = 'application/xml';
    response.body = xml(reqbody);
    if(error){
        console.log("Something went wrong!")
        console.log(error);
        console.log(body);
    } else{
        console.log(body);
        console.log(response.statusCode);
        console.log(error);
    }
});

Please be nice to me, even if I've made a noobish mistake. This is my first project in Node. :)

Upvotes: 0

Views: 903

Answers (1)

Joe
Joe

Reputation: 42646

You need to pass the data and header as part of the request, not on the Response.

request.post({
  headers: { 'Content-Type': 'application/xml' },
  url: link,
  body: reqbody,
}, function(error, response, body){
  console.log(body);
});

Also you don’t need to double encode your XML request body - you are (potentially dangerously) encoding the XML yourself, so no need to call xml(reqbody)...

Upvotes: 1

Related Questions