I have coded an HTTPS function using the native https module from NodeJS to get some JSON from an API.
When I'm doing a GET request I'm getting the JSON fine, but when I have to make a PUT request I got the JSON but I can't convert that into an object.
answer from GET:
{
items: [
{ dsname: 'A09999' },
{ dsname: 'A09999.ALLFILES.TEMP' },
],
returnedRows: 16,
JSONversion: 1
}
answer from PUT:
{
'cmd-response-key': 'C8124546',
'cmd-response-url':'https://myhost/zosmf/restconsoles/consoles/ibmusecn/solmsgs/C8124546',
'cmd-response-uri': '/zosmf/restconsoles/consoles/ibmusecn/solmsgs/C8124546',
'cmd-response':
' IEE457I 16.32.55 UNIT STATUS 464\r UNIT TYPE STATUS VOLSER' }
My code:
const makeRequest = options =>
new Promise(function(resolve, reject) {
let results = https.request(options, res => {
res.setEncoding('utf8');
let body = '';
res.on('data', data => {
body += data;
});
res.on('end', () => {
body = JSON.parse(body);
console.log('body :', body);
resolve(body);
});
});
if (options.body) {
// results.write(options.body);
results.write(JSON.stringify(options.body),['Transfer-Encoding', 'chunked']);
}
results.on('error', e => {
reject(e);
});
results.end();
});