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

javascript - nodejs http post request throws TypeError - Stack Overflow

programmeradmin1浏览0评论

I am trying to make a simple server that use google oauth (without express and passportjs, as I want to study the data exchanged).

When my program attempts to send a post request to google, nodejs throws:

http.js:593 throw new TypeError('first argument must be a string or Buffer');

I have checked and make sure that all parameters in query and option are all string, but the error still persist. What could I have missed here?

Here is my code:

// Load the http module to create an http server.
var http = require('http');
var url = require('url');
var fs = require('fs');
var querystring = require('querystring');
var content;

fs.readFile('./test.html',function(err,data){
    content = data;
});

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/html"});
  var path = url.parse(request.url).pathname;
  var query = querystring.parse(url.parse(request.url).query);
  var code;
  if (query!=null) {
    code = query.code;
  };

  if ('/auth/google/callback'==path){

    var data = querystring.stringify({
        'code':          ''+code,
        'client_id':     'id',
        'client_secret': 'secret',
        'redirect_uri':  'http://localhost:8999/auth/google/code/callback',
        'grant_type':    'authorization_code'
    });

    var options = {
        hostname: 'accounts.google',
        port:'80',
        path: '/o/oauth2/token',
        method: 'POST',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'Content-Length': ''+data.length
      }
    };

    debugger;
    var post = http.request(options, function(res){
        response.write(res);
        response.end();
    });
    debugger;
    post.write(data);
    debugger;
    post.end();
  }

  else if (path=='/auth/google/code/callback'){
    console.log(request.headers);
    console.log(request.url);
  }

  else response.end(content);

  console.log(request.headers);
  console.log(request.url);
});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8999);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");

Many thanks,

I am trying to make a simple server that use google oauth (without express and passportjs, as I want to study the data exchanged).

When my program attempts to send a post request to google, nodejs throws:

http.js:593 throw new TypeError('first argument must be a string or Buffer');

I have checked and make sure that all parameters in query and option are all string, but the error still persist. What could I have missed here?

Here is my code:

// Load the http module to create an http server.
var http = require('http');
var url = require('url');
var fs = require('fs');
var querystring = require('querystring');
var content;

fs.readFile('./test.html',function(err,data){
    content = data;
});

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/html"});
  var path = url.parse(request.url).pathname;
  var query = querystring.parse(url.parse(request.url).query);
  var code;
  if (query!=null) {
    code = query.code;
  };

  if ('/auth/google/callback'==path){

    var data = querystring.stringify({
        'code':          ''+code,
        'client_id':     'id',
        'client_secret': 'secret',
        'redirect_uri':  'http://localhost:8999/auth/google/code/callback',
        'grant_type':    'authorization_code'
    });

    var options = {
        hostname: 'accounts.google.',
        port:'80',
        path: '/o/oauth2/token',
        method: 'POST',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'Content-Length': ''+data.length
      }
    };

    debugger;
    var post = http.request(options, function(res){
        response.write(res);
        response.end();
    });
    debugger;
    post.write(data);
    debugger;
    post.end();
  }

  else if (path=='/auth/google/code/callback'){
    console.log(request.headers);
    console.log(request.url);
  }

  else response.end(content);

  console.log(request.headers);
  console.log(request.url);
});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8999);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");

Many thanks,

Share Improve this question asked Jul 11, 2014 at 5:37 MikeNQMikeNQ 6532 gold badges16 silver badges29 bronze badges 6
  • when are you making the post request ? – Mritunjay Commented Jul 11, 2014 at 5:56
  • post.end(), I assume, is when the request is sent. – MikeNQ Commented Jul 11, 2014 at 5:59
  • i think you are saying response.write(res) here res is a object see console.log(typeof(res)). – Mritunjay Commented Jul 11, 2014 at 6:01
  • Thanks alot Mritunjay, silly me, the content is a json object. – MikeNQ Commented Jul 11, 2014 at 6:14
  • did u got the solution of your problem? – Mritunjay Commented Jul 11, 2014 at 6:40
 |  Show 1 more ment

2 Answers 2

Reset to default 6

I think problem is when you are saying

response.write(res); //it needs a string

I think res is an object here.

try

response.write(JSON.stringify(res));

When you write response or request. It should contain string so you need to change it to

response.write(querystring.stringify(res)); 

or

response.write(JSON.stringify(res));
发布评论

评论列表(0)

  1. 暂无评论