1

我正在尝试使用 node.js 重现Python POST 请求(已验证工作)。这是一个相当简单的 POST 请求发送和接收 XML 数据。

但是,The request sent by the client was syntactically incorrect尽管我的 XML 有效(已验证)并且直接取自 Python 示例,但我还是遇到了错误。我究竟做错了什么?

我的代码(可重现):

// 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();

输出:

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.
4

1 回答 1

1

发现问题。我将请求从错误的示例发布到 URL!正确的path线是path: `/ws/rest/datadelivery/request?key=${api_key}`,.

于 2020-07-31T08:51:57.503 回答