Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

POST data from NodeJS with parameters and sessions

POST data from NodeJS with parameters and sessions

Problem

I'm writing user client for blog system. This is how I perform 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);
  });
});

However, everything seems to be ok, it gives me an error:

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)

What I'm doing wrong?

P.S. I've replaced my actual nickname with $my_nickname and real session with $SESSION-...

BTW when I access to google.com it works

Problem courtesy of: f1nn

Solution

All, I need to do was:

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

Now it works, although don't posts

Solution courtesy of: f1nn

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

POST data from NodeJS with parameters and sessions

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×