Skip to content
This repository has been archived by the owner on Dec 3, 2017. It is now read-only.

Cannot send json object #26

Open
chris-aeviator opened this issue May 16, 2016 · 5 comments
Open

Cannot send json object #26

chris-aeviator opened this issue May 16, 2016 · 5 comments
Labels

Comments

@chris-aeviator
Copy link

chris-aeviator commented May 16, 2016

Thanks for your great work, I like to use the microservice alot for different projects that do not require a backend but recent notifications.

To use the microservice for data-driven applications it would be benefitial if the user could payload some json data into the mail, as the underlying lodash library supports this.

I could not find out a way to transport e.g. a json object into the mailer (also html structure and "pure" data objects (just displayed as [object] ) fail with unexpected token responses). Further attempts in base64 encoding the object failed, as i get atob is undefined response from the server after i saved <%= atob(value) %> to the template.

I would appreciate any help and would be willing in contributing.

@coderbyheart
Copy link
Member

The mail enpdoint accepts any data, as long as it contains to (email) and name:

subject = _.template(template.subject)(req.body)
so you might be doing something wrong.

Can you show me your templates and the data you send?

atob is not implemented in Node.js, you need to define it:

if (!atob) {
  var atob = function (str) {
    return new Buffer(str, 'base64').toString('binary')
  }
}

@chris-aeviator
Copy link
Author

chris-aeviator commented May 17, 2016

Thanks for your very quick response 👍

Initially I was trying to submit a JSON object like

var x = {"to":"christopher@mydomain.cc","name":"Chris Test","email":"test@test.cc","order_details":{"items":[{"product":"Die Bodenstandige","price":69.99,"qty":1}]}};

and output it in the template with <%= order_details %> but it does not reach the template as the server responds with unexpected token i (coming from the "items" string ) .

Thanks for your hint with atob I ws not aware that such a simple Javascript function is not implemented in node.js :)

@coderbyheart
Copy link
Member

What's your template?

@chris-aeviator
Copy link
Author

chris-aeviator commented May 17, 2016

By now I'm just trying to output the json into the template, but what I want in the end is to create a HTML structure out of the ( JSON.parse 'ed data).

curl -v -X PUT http://server.xyz:3200/templates/projectOrder \ -H 'Content-Type: application/vnd.node-templater-mailer-microservice.v1+json; charset=utf-8' \ --data '{"subject":"Neue Bestellung von <%= name %>","html":"Toll, <%= name %> hat was bei uns bestellt! <h3>Der xxx geht an:</h3> <p>email: <%= email %></p><h3>Diese xxx verlassen uns:<%= order_details %><h3/><p>"}'

@chris-aeviator
Copy link
Author

chris-aeviator commented May 20, 2016

Hi…I decided to put the direct sending of the JSON data aside and tried to implement the atob way as it fit's my needs.

Still I cannot get the server/templating engine to accept atob.

I tried the following things:

Still in my template the field is processed with <%= atob(user_details) %> where user details is defined on the browsersite with var user_details = btoa(JSON.stringify(userDetails));

Inside the .js files I'm able to process a atob() command - it log's out perfectly to the console!

Can you please give me a hint where I need to define atob to be recognized by your module?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants