Anoop saju
Anoop saju

Reputation: 480

Error "Your client has issued a malformed or illegal request" while trying to get search results from youtube

I'm trying to get the page for the search results of youtube.Here I have input a video name and Iam trying to get the html page of search results.

I used the http module for sending and recieving data.

var youtube_query=querystring.stringify({
  'search_query':'bangarang', //video name
  'spf':'navigate'

});

var options_you = {                     //headers for searching in youtube

  host:'www.youtube.com',
  path:'/results',
  method:'GET',
  headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Content-Length': Buffer.byteLength(youtube_query),
      'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'


  }

};


function getvid_id(vid_result){  
                                              //callback function for finding the getting the result page
      vid_result.setEncoding('utf8');
      vid_result.on('data', function (chunk) {
     console.log(chunk);
    });      
}

var youtube_request = http.request(options_you,getvid_id);

youtube_request.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

    youtube_request.write(youtube_query);
    youtube_request.end();

This is the result page i have recieved

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-wi
dth">
  <title>Error 400 (Bad Request)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{backgrou
nd:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height
:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/error
s/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflo
w:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (m
ax-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0
}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo
_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-reso
lution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/
2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:ur
l(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png)
0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:ur
l(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png)
no-repeat;-webkit-background-size:100%
100%}}#logo{display:inline-block;height:54px;width:150px}
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>400.</b> <ins>ThatGÇÖs an error.</ins>
  <p>Your client has issued a malformed or illegal request.  <ins>ThatGÇÖs all w
e know.</ins>

Iam not able to understand the reason for the malinformed url

Upvotes: 0

Views: 2464

Answers (1)

Nija
Nija

Reputation: 188

Check your content-length line this is causing malformed url error

'Content-Length': Buffer.byteLength(youtube_query)

If you remove content-length it works but you may end up getting 301 as url is moved. So you may required to use follow-redirects module

var http = require("follow-redirects").http;

or you can use 'request' module

Upvotes: 1

Related Questions