Navigation

    APPDRAG Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular

    REMINDER

    Please be respectful of all AppDragers! Keep it really civil so that we can make the AppDrag community of builders as embracing, positive and inspiring as possible.

    Sending html mail from API

    General Discussion
    3
    7
    569
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Dick Honing
      Dick Honing last edited by

      I've tried reproducing code that works fine in one api to another(/new) api. I've checked and double checked, but I'm most likely overlooking something. From what I've concluded from my limited knowledge on debugging, is that request(urlTemplate) does nothing. And yes, the package 'request' is selected.

                  var urlTemplate = "https://api.appdrag.com/NewsletterViewer.aspx?newsletterID=2943&appID=webconnect-374b23"; // Replace with your own HTML template
                  //console.log(urlTemplate);
                  request(urlTemplate, {
                      encoding: null
                  }, function(error, response, body) {
                      console.log(response);
                      if (!error && response.statusCode == 200) {
                          if (response.headers['content-encoding'] == 'gzip') { // If template is located on AppDrag, file is GZIP by default
                              zlib.gunzip(body, function(err, dezipped) {
                                  emailContent = dezipped.toString();
                                  processTemplate();
                              });
                          } else {
                              emailContent = body.toString();
                              processTemplate();
                          }
                      } else {
                          callback(null, "File not found");
                      }
                  });
      

      Also, I noticed that the package 'request' is deprecated. Is this something I should worry about right now?

      Screenshot 2020-12-21 at 17.10.02.png
      https://www.npmjs.com/package/request

      1 Reply Last reply Reply Quote 0
      • Wassim
        Wassim last edited by

        Hi,

        We would need the whole code to help you debug it. Is process template in your function?

        Request is deprecated yes but for the use you have of it in this case (loading a remote url) you don't have to worry at all.

        1 Reply Last reply Reply Quote 0
        • Dick Honing
          Dick Honing last edited by

          @Wassim I sent you the code by mail ...

          1 Reply Last reply Reply Quote 0
          • Wassim
            Wassim last edited by

            Ok,
            Your issue is due to the use of ASYNC / AWAIT.

            Request is not promise based so you can't await it (and you don't do it at the moment, but your function is killed before it's called as it is async).

            You can either
            switch to a non async function globally and switch the rest of your code
            or
            follow this solution : https://stackoverflow.com/questions/41412512/node-js-promise-request-return

            1 Reply Last reply Reply Quote 0
            • Dick Honing
              Dick Honing last edited by

              @Wassim thanks for taking your time to help met out! I'm still learning JavaScript and the promise/async was still om my todo list. Now I think it's time to dive into this. I already collected some reading material, which might be interesting to others as well ...

              https://www.w3schools.com/js/js_promise.asp

              https://www.freecodecamp.org/news/javascript-from-callbacks-to-async-await-1cc090ddad99/

              https://medium.com/better-programming/callbacks-vs-promises-in-javascript-1f074e93a3b5

              https://medium.com/better-programming/should-i-use-promises-or-async-await-126ab5c98789

              1 Reply Last reply Reply Quote 2
              • Daniel Mulroy
                Daniel Mulroy last edited by

                @Wassim said in Sending html mail from API:

                https://stackoverflow.com/questions/41412512/node-js-promise-request-return

                @Dick-Honing I've used the package 'request-promise-native' which allows you to await a request.

                I recommend checking it out, it's been working great for me:
                https://www.npmjs.com/package/request-promise-native

                1 Reply Last reply Reply Quote 3
                • Dick Honing
                  Dick Honing last edited by

                  @Daniel-Mulroy thanks for the tip!

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post