I didn't eat because of your question T_T
As @hlfrmn said, you have a rare char in your Accept word!!
I reproduced the error:
const https = require('https');
//copied from origin question
var copiedOptions = {
headers: {
'Content-Type': 'application/json',
"Accept": 'application/json'
}
}
var writedOptions = {
headers: {
'Content-Type': 'application/json',
"Accept": 'application/json'
}
}
https.get('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY',writedOptions, (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
console.log(JSON.parse(data).explanation);
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
If you try with copiedOptions, you will get the same error.
Since Node v10.20.1
Searching in https://github.com/nodejs/node/tree/master and the stackTrace: _http_outgoing.js:472
at ClientRequest.setHeader (_http_outgoing.js:472:3)
I founded this:
const tokenRegExp = /^[\^_`a-zA-Z\-0-9!#$%&'*+.|~]+$/;
/**
* Verifies that the given val is a valid HTTP token
* per the rules defined in RFC 7230
* See https://www.rfc-editor.org/rfc/rfc7230#section-3.2.6
*/
function checkIsHttpToken(val) {
return tokenRegExp.test(val);
}
That regex ensure that header name only has allowed tokens or chars according to https://www.rfc-editor.org/rfc/rfc7230#section-3.2.6
Maybe a earlier validation in nodejs core could help:
You have a not allowed char in your header name!