Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js HTTP - TypeError: Header name must be a valid HTTP Token

when I'm trying to set the header in the following way ,it's working absolutely fine.

response.setHeader('Content-Type', 'application/json');

but when I'm trying to add variable instead of exact header name/value in the following way, it's showing error :-(

response.setHeader(result.headers);

if you console.log("headers -> " + result.header) the result would be the same.

headers -> 'Content-Type', 'application/json'

following are the exact error I'm getting , not able to figure out how to get around it.

 _http_outgoing.js:487

throw new TypeError(`Header name must be a valid HTTP Token ["${name}"]`);

^



 TypeError: Header name must be a valid HTTP Token ["'Content-Type', 'application/json'"]

at validateHeader (_http_outgoing.js:487:11)

at ServerResponse.setHeader (_http_outgoing.js:498:3)

at C:\service-mocker\src\main\app.js:54:22

at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)

Below is whole code I'm trying to implement :

var http = require('http');

var fs = require('fs');

var path = require('path');

var repl = require('repl');

var map={};

var key;

var value;



//create a server object:

var server = http.createServer(function(request, response) {



    fs.readFile("./resources/FileData1.txt", function(err, data) {

        if(err) throw err;

        content = data.toString().split(/(?:\r\n|\r|\n)/g).map(function(line){

            return line.trim();

        }).filter(Boolean)

        var result = processFile(content);

        console.log("url -> " + result.url);

       console.log("status -> " + result.status);

       console.log("headers -> " + result.headers);

       console.log("body -> " + result.body);



       function objToString (obj) {

        var str = '';

        for (var p in obj) {

            if (obj.hasOwnProperty(p)) {

                str +=  obj[p] + '\n';

            }

        }

        return str;

    }

    function processFile(nodes) {



        nodes.forEach(function(node) {

            if(node.startsWith("//")){

                key = node.substring(2, node.length-2).toLowerCase().trim();

                return;    

            }

            else{

                value = node;

            }

            // map[key] = value;

            if(key in map){

                map[key].push(value);

            }else{

                map[key]= [value]; 

            }

        });



        return map;

        // console.log(map);

    }

   if(request.url == result.url ){

        response.setHeader(result.headers);

        // response.setHeader('Content-Type', 'application/json');

        response.write( objToString(result.body) );

        response.statuscode = parseInt( result.status );

        response.end();

    }else {

        // response.writeHead(404, {"Content-Type": "text/html"});

        response.end("No Page Found");

        }

      });

 });


var port = process.env.PORT || 8080;

server.listen(port, function() {

    console.log('Listening on port ' + port);

});
like image 549
Fatma Zaman Avatar asked Jun 14 '26 09:06

Fatma Zaman


1 Answers

It looks like result.headers is returning a single string, but response.setHeader needs two arguments: the header name and the header value.

Based on the error message, you're doing the equivalent of:

response.setHeader("'Content-Type', 'application/json'");

Instead, you need to split out the header into key and value:

//n.b. make sure that your result only has the one header!
var headerSplit = result.headers.split(',');
var headerKey = headerSplit[0];
var headerVal = headerSplit[1];

response.setHeader(headerKey, headerVal);
like image 93
Brian Avatar answered Jun 17 '26 00:06

Brian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!