最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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 onplete] (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);

});

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 onplete] (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);

});
Share Improve this question edited May 18, 2018 at 20:09 Fatma Zaman asked May 18, 2018 at 19:43 Fatma ZamanFatma Zaman 691 gold badge2 silver badges11 bronze badges 4
  • Where's your code there's something wrong with your strings. – Darkrum Commented May 18, 2018 at 19:49
  • the code is reading the file in the below format : //URL// account/42 //Status// 200 //BODY// { "name": "XYZ" } //HEADERS// – Fatma Zaman Commented May 18, 2018 at 20:11
  • And your file looks like what? – Darkrum Commented May 18, 2018 at 20:13
  • Simple Text file which has all the information. – Fatma Zaman Commented May 18, 2018 at 20:38
Add a ment  | 

3 Answers 3

Reset to default 4

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

If you dont want to set headers one at a time with response.setHeader, you should use response.writeHead, which will write the status code and your headers into the response.

response.writeHead(200, headers)

Here headers must be a JavaScript object.

Replace

response.setHeader(result.headers);
response.statuscode = parseInt( result.status );

with

response.writeHead(parseInt(result.status), parseHeaders(result.headers));

where parseHeaders is a function that parses result.headers into an object.

function parseHeaders(headers) {
    var o = {};
    headers.forEach(header => {o[header.split(',')[0]] = header.split(',')[1]});
    return o;
}

In this answer I've assumed result.headers is formatted like so

[
    "header1, header1value",
    "header2, header2value"
]

Template literals use a back-tick not quotes.

发布评论

评论列表(0)

  1. 暂无评论