3

i want to make a request to a page using request module in node js, but this have a error 500 . This is part of the json response

{
  "readable": false,
  "domain": null,
  "_maxListeners": 10,
  "httpVersion": "1.1",
  "complete": true,
  "_pendingIndex": 0,
  "url": "",
  "method": null,
  "statusCode": 500,
  "_consuming": true,
  "_dumped": false,
  "httpVersionMajor": 1,
  "httpVersionMinor": 1,
  "upgrade": false,
  "body": "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>\n<title>500 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\n<p>The requested\n                site <a href=\"http:// HTTP/1.1/\"> HTTP/1.1</a> is not presently available on\n                this <a href=\"http://www.hybrid-cluster.com/\">web cluster</a>.\n                This may be a temporary issue, so please try again in a few\n                moments.</p>\n\n</body></html>\n",
  "_readableState": {
    "highWaterMark": 16384,
    "length": 0,
    "pipes": null,
    "pipesCount": 0,
    "flowing": false,
    "ended": true,
    "endEmitted": true,
    "reading": false,
    "calledRead": true,
    "sync": false,
    "needReadable": true,
    "emittedReadable": false,
    "readableListening": false,
    "objectMode": false,
    "defaultEncoding": "utf8",
    "ranOut": false,
    "awaitDrain": 0,
    "readingMore": false,
    "decoder": null,
    "encoding": null,
    "buffer": []
  },

This is the code that i use for the request

app.get('/', function(req, res){
    var options = {
        url: 'http://www.metallicabyrequest.com/'
    }
    request(options, function (error, response, html) {
        /*if (!error && response.statusCode == 200) {
            res.json(html);
        }*/
        if(error){
            res.json(error);
        }else{
            res.json(response)
        }
    });
});

Is there any way to get prevent the error 500? I read that if you change the headers, is probably that the request works, but i don't know how to do ...

Jose Sosa
  • 2,157
  • 3
  • 17
  • 18

1 Answers1

4

That site require 'Host' in request header:

var options = {
    url: 'http://www.metallicabyrequest.com/',
    headers: {
       'Host': 'www.metallicabyrequest.com'
    }
}
damphat
  • 18,246
  • 8
  • 45
  • 59