Search code examples
node.jsxmlpostrequestnode-https

Send POST request with XML data in node.js: Error 400, "The request sent by the client was syntactically incorrect"


I'm trying to reproduce this Python POST request (verified working) with node.js. It is a fairly simple POST request sending and receiving XML data.

However, I am getting The request sent by the client was syntactically incorrect errors, despite my XML being valid (verified) and taken directly from the Python example. What am I doing wrong?

My code (reproducible):

// DOC:
// https://wiki.solargis.com/display/public/WS+API+technical+documentation#WSAPItechnicaldocumentation-DatadeliveryWebservice(APIforgettingtimeseriesdata)
// https://nodejs.org/api/http.html#http_http_request_options_callback
// https://nodejs.org/api/http.html#http_request_write_chunk_encoding_callback
// https://nodejs.org/api/https.html

let https = require('https');

const api_key = 'demo';

var request = '<?xml version="1.0" encoding="UTF-8"?>' +
'<ws:dataDeliveryRequest dateFrom="2014-04-28" dateTo="2014-04-28" ' +
    'xmlns="http://geomodel.eu/schema/data/request" ' +
    'xmlns:ws="http://geomodel.eu/schema/ws/data" ' +
    'xmlns:geo="http://geomodel.eu/schema/common/geo" ' +
    'xmlns:pv="http://geomodel.eu/schema/common/pv" ' +
    'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
        '<site id="demo_site" name="Demo site" lat="48.61259" lng="20.827079"></site>' +
        '<processing key="GHI" summarization="HOURLY" terrainShading="true"></processing>' +
'</ws:dataDeliveryRequest>';

var request_utf8 = Buffer.from(request, 'utf-8');

let options = {
  host: 'solargis.info',
  path: `/ws/rest/pvplanner/calculate?key=${api_key}`,
  headers: {
      'Content-Type': 'application/xml',
    //   'Content-Length': Buffer.byteLength(request),
    //   'Transfer-Encoding': 'chunked', //See https://nodejs.org/api/http.html#http_request_write_chunk_encoding_callback
    },
  method: 'POST',
};

const req = https.request(options, (res) => {
    console.log(`STATUS: ${res.statusCode}`);
    console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
    res.setEncoding('utf8');
    res.on('data', (chunk) => {
        console.log(`BODY: ${chunk}`);
    });
    res.on('end', () => {
        console.log('No more data in response.');
    });
});

req.on('error', (e) => {
    console.error(`problem with request: ${e.message}`);
});

// Write data to request body
req.write(request_utf8);
req.end();

The output:

STATUS: 400
HEADERS: {"date":"Wed, 29 Jul 2020 10:00:07 GMT","server":"Apache","set-cookie":["JSESSIONID=4F3F9848F6E115329F6E00624341EB2E.balanced_ws1; Path=/ws/; Secure; HttpOnly"],"content-length":"968","connection":"close","content-type":"text/html;charset=utf-8","x-pad":"avoid browser bug"}
BODY: <html><head><title>Apache Tomcat/7.0.41 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 400 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The request sent by the client was syntactically incorrect.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.41</h3></body></html>
No more data in response.

Solution

  • Found the issue. I was posting the request to the URL from the wrong example! The correct path line is path: `/ws/rest/datadelivery/request?key=${api_key}`,.