Kunok Kunok - 1 year ago 161
HTTP Question

Node.js when sending POST request with base64 decoded characters, invalid characters error appears

I am trying to send POST request with base64 decoded string, however I get invalid characters error.

I have this block to generate base64 decoded string:

var domain = "domain.com";
var decoded = Buffer.from("x3abaARV"+domain+"1cYNYtk", 'base64').toString('ascii');

and then I sent POST request using
module from npm:

var headers = {
ip: "",
key: decoded,
domain: domain
var options = {
url: 'http://somedomain.com/foo/bar/example.json',
method: 'POST',
headers: headers,

request(options, (error, response, body) => {
if (error) {

which returns:

ypeError: The header content contains invalid characters
at ClientRequest.OutgoingMessage.setHeader (_http_outgoing.js:358:11)
at new ClientRequest (_http_client.js:86:14)
at Object.exports.request (http.js:31:10)
at Request.start (/home/kuno/code/aviokarte-web/node_modules/request/request.js:744:32)
at Request.end (/home/kuno/code/aviokarte-web/node_modules/request/request.js:1435:10)
at end (/home/kuno/code/aviokarte-web/node_modules/request/request.js:566:14)
at Immediate. (/home/kuno/code/aviokarte-web/node_modules/request/request.js:580:7)
at runCallback (timers.js:637:20)
at tryOnImmediate (timers.js:610:5)
at processImmediate [as _immediateCallback] (timers.js:582:5)

How do I properly set this up?

Answer Source

If you inspect the value of decoded, you will find that there are non-printable characters, which are not allowed in HTTP header values. You will need to use some kind of binary-safe encoding (such as base64 or URL/"percent" encoding) for that kind of data.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download