使用参数和会话从NodeJS POST POST数据

时间:2012-06-30 10:44:37

标签: node.js post request

我正在为博客系统编写用户客户端。这就是我执行请求的方式

var http = require('http');
var destination='http://darkdiary.ru/users/$my_nickname/add';
var post_data = '_action=1&id=&title=message_subject01&text=message_text01&font=&size=100%25&color=&privacy=private&comment_privacy=all&tag=&tag_old=&music=&mood=&subscribe=1&prev_subscribe=';
var server = http.createClient(80, destination);
var request = server.request('POST', destination, {
    'host': destination,
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11',
    'Content-Length': post_data.length,
    'Cookie': 'anonym_login=$anonym_nickname; sk=$SESSION-f72cd7c23b01f2a9fe6943bd9e'
    });
request.end(post_data,  encoding='utf8');
request.on('response', function (response) {
  console.log('STATUS: ' + response.statusCode);
  console.log('HEADERS: ' + JSON.stringify(response.headers));
  response.setEncoding('utf8');
  response.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

然而,一切似乎都没问题,它给了我一个错误:

events.js:48
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: Parse Error
    at Socket.socketOnData (http.js:1288:20)
    at TCP.onread (net.js:374:27)

我做错了什么?

P.S。我用$ my_nickname和真实会话用$ SESSION -...替换了我的实际昵称。

当我访问google.com时,

BTW

1 个答案:

答案 0 :(得分:1)

所有,我需要做的是:

var destination='www.darkdiary.ru';
...
var request = server.request('POST', '/users/%my_nickname/add', {

现在它有效,但不发布