[SOLVED] Get a website html content to a template with HTTP

Hello,

I’d like to get the HTML content of a website (server side to prevent CORS policy problem) when the address is typed on an input, and give it to the template (I plan to parse the <title></title> but no help needed for this).
I tried many different ways but I’m getting confused. I’m looking for a hint

Here is my current code:

imports/ui/input.html:

<template name="input">
    <input type="text" name="link" placeholder="http(s)://..." />
    <input type="text" name="description" placeholder="description" value="{{curl}}" />
</template>

imports/ui/input.js:

Template.input.helpers({
    curl() {
        return Session.get('curl');
    }
});

Template.input.events({
    'keyup [name="link"]'(event){
        Meteor.call('input.curl', event.target.value, function(error, result){
            if(error)
                uiCallback(error)
            else{
                console.log(result);
                Session.set('curl', result);
            }
        });
    }
});

imports/api/categories.js:

if (Meteor.isServer) {
    Meteor.methods({
        'input.curl'(link){
            let ret = HTTP.post(link, (error, result) => {
                console.log(result.content);
                return result;
            });
            return ret;
        }
    });
}

Thank you :smile:

You are attempting to use an asynchronous callback and return the result synchronously. That won’t work. However, thanks to the magic of Meteor, you can just write the whole thing synchronously:

  'input.curl'(link) {
    try {
      const result = HTTP.post(link);
      console.log(result.content);
      return result;
    } catch (error) {
      throw new Meteor.Error(error.message);
    }
  }

The other thing I’m a little worried about is the use of keyup on the input field. That will fire the Meteor.call on every keyup, which will likely be multiple times. If you must use keyup, you should take steps to debounce your typing.

Thank you very much for your answer, it works!
I plan to filter link with a regex to call HTTP only when it looks like a proper URL. I’ll just wrap it around an if() statement.