RDD
RDD

Reputation: 31

HTTP post call from Template.event to a route in iron:router + meteor

I want to make HTTP.call with post parameters from Template.event in meteor. I have defined the route in iron:router route of my current application. The route is getting the call but I am not able to get the post parameters. The route is a server side route and returns the pdf content using :

Template.eStatement.events({
    'click .pdf': function (event, template){ 
         event.preventDefault(); 
         param = Some json object that I need to pass as post parameter.
         HTTP.call("POST", '/statement', JSON.stringify(param), 
         function(error, result){ if(result){ // } if(error){ // } //done(); }); }}); 

This is my route in (I am using iron:route package for meteor)

Router.route('/statement', function () { 
var param = JSON.parse(this.params.query.param); 
/** Get the pdf content by calling the api 
/** Write the content back : 
this.response.writeHeader('200', { 
'Content-Type': 'text/html', 
'Content-Disposition': "inline", 
});
this.response.write('pdfcontent');
this.response.end(); },{where: 'server'}

Upvotes: 0

Views: 250

Answers (1)

CaptSaltyJack
CaptSaltyJack

Reputation: 16025

Try something like this instead:

On the client: (within the client/ folder)

Template.eStatement.events({
  'click .pdf': function (event, template) {
    var params = {
      something: 'abcdef',
      someOption: true
    };
    HTTP.call('POST', '/statement', {
      data: params
    }, function (error, result) {
      console.log('http callback');
      console.log(error);
      console.log(result);
    });
  }
});

On the server: (within the server/ folder)

Router.route('/statement', {
  where: 'server',
  action: function () {
    var params = this.request.body;

    // do something with params

    this.response.writeHeader('200', {
      'Content-Type': 'text/html',
      'Content-Disposition': "inline"
    });
    this.response.write('pdfcontent');
    this.response.end();
  }
});

And keep in mind that, in the route, this.request.body is an object in this case, not a string. So you don't need to use JSON.stringify and JSON.parse to handle that.

Upvotes: 1

Related Questions