GMail API using POST request, error code: 400, recipient address required - node.js

I am trying to send an email using POST request with just Node standard modules in NodeJS v8.10 via GMail API.
The sample code is given below.
I am getting an error:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalidArgument",
"message": "Recipient address required"
}
],
"code": 400,
"message": "Recipient address required"
}
}
It says recipient address required but according to what I think, (I may be wrong), my base64url conversion is proper since I checked it in Google API Explorer but my problem is in passing the data, I am doing it properly as told in the guide i.e. inside body with 'raw' key but it still does not work and this is where my problem must be. Maybe I am missing something, maybe I do not know the proper structure.
Yes, there are multiple posts regarding this but none of them provided solution.
I referred the guide on https://developers.google.com/gmail/api/v1/reference/users/messages/send
but the given example is of with the use of client library.
I tried everything, passing the 'raw' with base64url encoded data into write function of the request, passing it as a data parameter in options, passing it through body parameters in options, everything I can think of.
Am I missing something? Where am I going wrong?
I am a newbie in nodejs so please explain and if possible, an example structure of solution would be most welcome.
Base64url produced is working fine, I guess. I copied the string produced by conversion and tried it at https://developers.google.com/gmail/api/v1/reference/users/messages/send?apix=true
It works fine and sends me the mail but it does not work on my code.
var email = (
"Content-Type: text/plain; charset=\"UTF-8\"\n" +
"Content-length: 5000\n" +
"MIME-Version: 1.0\n" +
"Content-Transfer-Encoding: message/rfc2822\n" +
"to: something#something.com\n" +
"from: \"Some Name\" <something#gmail.com>\n" +
"subject: Hello world\n\n" +
"The actual message text goes here"
);
async function sendMail(token,resp) {
return new Promise((resolve,reject) => {
var base64EncodedEmail = Buffer.from(email).toString('base64');
var base64urlEncodedEmail = base64EncodedEmail.replace(/\+/g, '-').replace(/\//g, '_');
var params = {
userId: 'me',
resource: {
'raw': base64urlEncodedEmail
}
};
var body2 = {
"raw": base64urlEncodedEmail,
}
var options = {
hostname: 'www.googleapis.com',
path:'/upload/gmail/v1/users/me/messages/send',
headers: {
'Authorization':'Bearer '+token,
'Content-Type':'message/rfc822',
},
body: {
"raw": base64urlEncodedEmail,
'resource': {
'raw': base64urlEncodedEmail,
}
},
data: JSON.stringify({
'raw': base64urlEncodedEmail,
'resource': {
'raw': base64urlEncodedEmail,
}
}),
message: {
'raw': base64urlEncodedEmail,
},
payload: {
"raw": base64urlEncodedEmail, //this is me trying everything I can think of
},
// body: raw,
// }
userId: 'me',
// resource: {
// 'raw': base64urlEncodedEmail
// },
method: 'POST',
};
var id='';
console.log(base64urlEncodedEmail);
const req = https.request(options, (res) => {
var body = '';
res.on('data', (d) => {
body += d;
});
res.on('end', () => {
var parsed = body;
console.log(parsed);
})
});
req.on('error', (e) => {
console.error(e);
});
req.write(JSON.stringify(body2));
req.end();
});
};
Thank you for your time and answers.

I found the solution.
It says everywhere to convert the rfc822 formatted string to Base64url to send and attach it to 'raw' property in the POST body but I don't know what has changed and you don't need to do that anymore.
First things first, the Content-Type in header should be
'Content-Type':'message/rfc822'
Now, since we are specifying the content-type as message/rfc822, we don't need to convert the data we want to send into base64url format anymore, I guess (Not sure of the reason because I have a very little knowledge about this.)
Only passing "To: something#any.com" as body works.
Here is the complete code of how to get it done for someone who is struggling for the same problem.
function makeBody(to, from, subject, message) {
let str = [
"to: ", to, "\n",
"from: ", from, "\n",
"subject: ", subject, "\n\n",
message,
].join('');
return str;
}
async function getIdAsync(token,resp) {
return new Promise((resolve,reject) => {
let raw = makeBody("something#gmail.com", "something#gmail.com", "Subject Here", "blah blah blah");
var options = {
hostname: 'www.googleapis.com',
path:'/upload/gmail/v1/users/me/messages/send',
headers: {
'Authorization':'Bearer '+token,
'Content-Type':'message/rfc822'
},
method: 'POST',
};
const req = https.request(options, (res) => {
var body = '';
res.on('data', (d) => {
body += d;
});
res.on('end', () => {
var parsed = body;
console.log(parsed);
})
});
req.on('error', (e) => {
console.error(e);
});
req.write(raw);
req.end();
});
};
Happy Coding :)

Related

AWS Using Lambda to Get S3 Object

I have put together an AWS API Gateway (HTTP) that triggers a lambda function. The lambda function gets an object from an S3 bucket. I've followed the docs from AWS, and I'm getting the objects body contents successfully but the PDF is coming through corrupted.
I have also reviewed this post on AWS, but for some reason when I add isBase64Encoded: true the response returns as 500 and no detail gets logged to CloudWatch.
const getSupportingDocs = async (doc_id) => {
try {
const streamToString = (stream) =>
new Promise((resolve, reject) => {
const chunks = [];
stream.on("data", (chunk) => chunks.push(chunk));
stream.on("error", reject);
stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
});
const command = new GetObjectCommand({
Bucket: "deq-waterlink-at",
Key: doc_id,
});
const { Body } = await s3Client.send(command);
//const { Body } = await s3Client.send(command).promise(); -> tried & nothing happened
const bodyContents = await streamToString(Body);
//console.log(bodyContents);
return bodyContents;
}
catch (err) {
console.log("ERROR DOWNLOADING: ", err);
}
};
exports.handler = async (event) => {
console.log("EVENT: ", event);
let response;
try {
if (event.queryStringParameters) {
let key = event.queryStringParameters['doc-id'];
let doc = await getSupportingDocs(`interactiveMap/${key}`);
response ={
"statusCode":200,
"body": JSON.stringify(doc),
/*"isBase64Encoded": true, --> Results in an Internal Server Error*/
"headers": {"content-type": "application/pdf"}
};
}
else {
response = {
statusCode: 200,
body: JSON.stringify('No Document ID included'),
};
}
return response;
}
catch (err) {
console.log("ERROR: ", err);
}
};
UPDATE:
After following the suggestion by Daniel Seichter below I was able to get the download triggered but I get an error message when trying to open the PDF that the file is damaged and cannot be repaired. If I removed the JSON.stringify around doc that file downloads but is blank.
I can directly download the file from S3 and it is not corrupted so something is happening in the download process.
I can log the response that the function generates and get the response below, but the browser returns a 500 response.
2022-08-03T21:56:12.163Z 9bd65c5c-6742-45a3-a6ef-6f314e4efa89 INFO RESPONSE: {
statusCode: 200,
headers: { 'content-type': 'application/pdf' },
isBase64Encoded: true,
body: '"%PDF-1.7\\n\\n4 0 obj\\n(Identity)\\nendobj\\n5 0 obj\\n(Adobe)\\nendobj\\n8 0 obj\\n<<\\n/Filter /FlateDecode\\n/Length 35942\\n/Length1 67496\\n/Type /Stream\\n>>\\nstream\\nx��\\u0007X\\u0014��?~�ݙ��.\\u001d�]\\u0016\\u0010\\\\p\\u0017V���bÂ�\\u00114(\\b(��J�+D�5�\\u0016M��ILL̛\\u00045�E\\r��Ę^Mb��f�2�;wv�\\u0012�����<�����a>s��{��{Ν\\u0001�\\u0000�\\u0019�\\u0003O^Ѱ!m�[;\\u0000\\u0016F\\u0001D?6$/\x7F0��\\u000b\\u00006���z\\f)\\u001c]t��u\\u0014�\x7F\\u0004(=2�h\\\\΢w�g\\u0003�܎�l\\u001f]�Lm�d�\\t\\u0000�\\"�/�&odI�O��\\u0000BN\\u0001�\'*�*�\\u0003NNa\\u0001�^�2\'+�4Yg�x�\\u0006�\\u0017�g�S���~�=U�Ma}�ߦU4z!\\flH\\u000f�\\u0001ô�󧖏��\\u0018#�G\\u0000}~��������X�\\b�O��\\u0004c��[��a<���i^����\\u0000�\\u0004��ٙ��\\u0015k�.l\x7F\\n��:UW1�+�Q���\\u000f`yk]uSE��\\\\\\u000bDY���Y\\u0015uպ��4\\u0001��\\u0007к���M�yp\\u001c`�)Z��P��\\u001b��\\u0001���y��K�xၭ������B�\\u0000�xxf�\\u001f�����s�w��> �Dz\\"0�?�9A-�\\u0005�eb�l�\\u0003rM\\u0017\\u001d�i��7C\\u001f`�\\u0004\\u0006\\f��\\tȵY���\\u0014�[�\\u001c\\u0002\\u001e\\u0004�vލU����68��\\u00020j�cy�c�m��y�:�r�>8��j\\u0005<�q�\\u001eR)�,��#V w��V��hO�\\u0015\\u0002$a\\u001b�%��\\u001dp��+���o�W���wa\\u0016�\\u000f\\u001e�t�\\f<��a�b;\\u001c����\\u0007�(\\u0016�4��I�P.���p8�܈y��\\u0011����wE\\u000e�=\\nu|\\u0013$*Z�A�&*!��\\r~\\u0015��\\u000f�\\u0004��7�N�q(��\\u0004�\\u0010J�\\np��=�\\u0013y���,�w\\nsa\'M���w�t�/|�I��އ�큻i��\\u001dH�\'#\\u0004�\\u0006=��\\u0018�\x7F|p/�f�Z��������\\u001e8��q��z\\\\=�\\u001eW������q��z\\\\=����vp\\b�����۾\\u0004�\\u000b�|$l���z�\\u0001����\\f���\\u0000���a;�;,��5c�tخX�y{��?\\u0001^^��r�L��\\txŢϻ��)�\\u0003r��`�\\u0013A�~3\\\\=�\\u001eW��\\u001d�\\u001e^��<�<��˺z�� #�\\u0001\\u001a�C��\\u0000�O\\u0002\\u0011T�*\\u0019ՠFԀ\\u0006Q\\u000bZ_\'�#��\\u0007=�AF#\\u0018\\u0011� �w\\u001eL`B4C0b��!\\u0010�\\u0018\\n��s\\u0010\\u0006a��\\u0010�\\u0018\\u0001���2FA��/��h�\\u001e�\\u0003�\\u0002VD��6����\\u0018�A��\\u001d1\\u0016�\\u0010� \\u001e1\\u001e�\\u000f�\\t=\\u0011\\u0013 \\u00011\\u0011\\u0012\\u0011{�\\u0003с�;$A\\u0012b2$#��ވNp!� ��\\u001b�Ș\\n��np#��>�i���\\u0015�è\\f�L�D̂,ľ��w\\u0016�A\x7F��0\\u0000q��\\u0003a b6d�~\\u0001\\u000f\\fB\\u001c$c\\u000e� �B.b\\u001e��~�|\\u0018�8\\u0018� \\u000e�q(\\fE\\u001c\\u0006�|?�p\\u0018�X\\u0000#\\u0010G�Hđ2��Q�\\u001fa4�F,�1�c`,�X�\\u001f�\\b�\\u0010��\\u0018q\\u001c�C�\\u0006�#��\\u0012�\\u0019\\\\a(�B)�\\u0004��8\\u0011�E�\\u0016�|��L�I0\\tq2LF,�r�\\n���\\u001e��X\\t��UP�X\\rՈSa��;�\\u00065�52�B-�t��8\\u0003f����P�X\'�,��X\\u000f��^�����Ѐ� c#4\\"6A\\u0013b34���90\\u0007q.�C�\'�|���\\u0000\\u0016�����\\u0010q\\u0011,F\\\\,�\\u0012X��\\u0002-�/�\\u0015Z\\u0011����Ka\\u0019�2\\u0019۠�w\\u0012���\\u0011��r�\\u0015�\\u0012q���`��\\u000bX\\r�\\u0011��\\u001a�\\u001b�F�\\u001b�&ě\\u0010?���f�[�\\u0016ĵ�\\u0016q\\u001d�G\\\\��\\u0019��\\u001b\\u00107�F�[�V�M�\\u0019q3��;\\u0001��x;lA�\\"�\\u001dp\\u0007�V���)�)�]�\\rq��w�݈����O�^؎�]���~��e�\\u0001;|\\u001f�\\u0003� �2\\u0010\\u001f��?�?|\\u001f���\\b�#�(�Ў�.�.���\\u0010v�n�=�\\u0018�c�8��\\u0017q/�\\u0007�\\u000f�!�\\u0003�\\u0007� �Ax\\u0002�\\t���\\u0010\\u001cB<\\f�\\u0011����OB\\u0007b\\u0007<�{\\u000f���ix\\u001a�\\u0019x\\u0016�Yx\\u000e�9�w�yx\\u001e�\\u0005x\\u0001�Ex\\u0011�%x\\u0019�ex��\\u000e�\\u0002�\\"�*�k�\\u001a�Qx\\u001d�ux��6�!�18��&��x\\u001c�#�\\u0005o���\\u001dx\\u0017�]\\u0019߃�\\u0010߇\\u000f|��\\u0003�\\u0010�C�\\b�#\\u0019?��\\u0011?�O|o§p\\u0002񄌟�爟��\\u0005|�;\\u0006\'�K�/�+į�kįe�\\u0006��\\u0001�·�������x\\nN!��Ӿ��\\f�A�\\u0001~D�\\u0011~B�\\t~F�\\u0019�(�\\u0002� ������o���#�\\u0006\x7F�\\u001f�\x7F\x9F�\x7F�9�sp��*��N�N�\\u0010%\\u0019}���wML�5�\\u0019X\\u001a\\"\\u0011x)��u\\u001aa\\u0018�~�vaM�¼\\u001cTk���b�\\u001d�\\u0010�3!�\\u0017j��NW\\n�ʹ�Lԉ�\\u0004�#/\x7F�Æ\\u0017�\\u0000\\u0018]8fl\\u0011��f|\\tL�xm\\u0019�7��w^\\u0012e�P�Ɏ\x7F[\\u000b��\\u0005H�\\u0015C\\u001a�\\u0018\\\\/�QS\\u000fA�;\\n��8Ԙ\\u0013P�yQ\\u000b-�y�\\u0013g��s��\\u0007 �N�K�b�\\u0002,=V.]�z�\\u00015UWi\\u001c\\u0002�\\u0017������I���p�Kځ=Fg\\u0018�&{ܓS:��h��\\u0011Æff���q�������\\u001c�\\u0012\\u0013z����clVK��Ȉ�А`�)�h��\\u001a�J\\u0014�\\n�c\\u0019\\u0002I�=,�dW��\\u0011i��J�\\u0003�K��l��g[;\\u0004]R(򲇢.�G_\\u0016��\\u001d\\u001f�\\u000e�����<Z�.\\u0018�U;�ډ�\\u001dh+�4\\u0012[\\n<�_5ݞ_�\\u001e�[U^�O��\\r���?:\\u0003��u�R�r��ժ�$إRcP�!,��E\\u0006\\u000f$r�\\u0019��w\\u0017\\u0003�69�=������kz�gM9\\u0006�yX\\u0013�.���u�pq\\u0016�c]!�?D�\\u0015��J�]km���\\u001d�Xw%u��a�\\u0001��;4U���k�s\\u0015H�.`��k�)\\u001f��U^cm�r\\u0019\\"1Ś_c]m��ȯ)G���SWL��ܒ\\u0015���� ��\\u001b\\u001d�C�Đ\\u0005\'#���a�V\\u001a]�z��}ۘ��sm\\u0014KKKÐ���v�\\u0010+˟��]\\ts&\'��\\u0014`#U�t���\\nJg�t��5�2�7�4�E�kp`*�]�ի���U\\u0015U9��s�=��\\r�\'��\\u001dD��\\u0006�\\u0002\\u00050��s��Jm~f\\u0017�-ɥ��+�\\"��ޝR\\u001eH����L+�`\\u0018V�n�����\\u0012;\\u0016ͤP�\\t�+3e᱕\\u0012|���S�|��n]�+��r��S��T\\u0004R\\u0014q�_�\\u0006\\u0007�\\u0007��^=�n\\u001d��|u�~_�\\u0014��`_���`�7�\\u001c[-,����\\u000e��l\\u001f|Ci�����E�S\\t\\u0018<�$;�f,�\\u0016vE\\u0001E\\n\\u0005K-w\\u0007��?�\\u00027�2\\u0014�جȨq%��ȧ\\u0012\\u001a.ư�N\\u0005\\t\\u00057\\u0013�8�6ʣ��n��\\u0006�6\\u001b��5�=0\\u0005#��cJ�q+L��\\r\\u001e�\\u0003ǣ��tt�\\u0004��9�]9ݏ�۱��d\\u0005\\u0016�.�w��\\r!�����$�_dW���M�%l$S�\\u000f1�,\\r�\\u001c8����:0��X�������h�K:\\"��Z\\rF�\\u0000t��\\u0005c&�X�WwK�?%�S*\\u0007(����Ձ�D���2�`+(�0�8M�:���?>�Kqa����ǻ�\\u0005A\\u0001)(F)G=���NV���!+�&�\\u001c�5Ժ��d7C����]��Wr�\\n��S\\u0019�J\\u0013i�J#���\\u0018\\u0011��\\u0007<\\u0000�r.\'\'����\\u0004�4�+�#�~Ɵf�7\\u0014/7��E�r?���t��0M��i�\\u000bhw=*�#xD���2��\\bMڍ)\\u0007q\\u0018E\\u0002{4DK\\"w�Sc����u����h�\\u0012\\u001e?�+�]hz܄�=\\u001a��dĆr��\'�\\u0006YYb��[��=�%�JkV��R\\u001d��2��P�Xb�\\u001bڭ\\u0011/E�6|Y�\\f��X<DVA/\\u0012\\u0005�H���|�A�$\\u000eRI,�cѫU��\\u000e6�\\u001e��\\u0003\\u000b�\\u0010H/\\r�W�\\n��,d<\\f%�`�\\u001a\x7F:��\\"+\x7F K0�\\u0018�80e\\u0011Y�+��,ĸ\\u0011�\\u000b\\u0002���<�D-d\\u001eƣ�>7p�C&�m6�\\u0006�ހ��x�\\u001dh�\\u001b��\\u0007�3\\u0003�3\\u0002��{m�^\\u0013�W��\\u0019-�Ie��)�bw�ų���1���}�Ɛ>HG�\\u0003tL\\u000e�\'��\\u000eR�GkH�\'1�~?\\u0019�}4a�G�6�,����z��\\u0010\\u0019��-��D\x7Fj\\u0002M�O�w��M���7�n��Bi�!\\"\\u0006x*�3��̈�!438Ј�_��߈\\u0002S9L�w\\u0013��y��|�u��믌\\u0016Tb��w�/��ɻ댖��z\\u0015�W�z\\u0019���z\\n�\\u0007��Z����-V�m[\\u0012,[�EZ~�\\u001clٱ9ܲis/˭��,\\u001b1��L6cq��dúp��u\\u000e��u6\\u000b�#��kש\\r��C�C�C��\\t\\u0002\\u0007\\f\\u0007\\u0018=ήlj���?\\u0018����=��-�\\u0012�Y�Y�z��\\f�<�}j�)����mf��\\u0004��=F�sO���vo��-�˓��/�r��\\r�y\\u001a;B\\u001b�=��7[z[���F���z��ҁדx�|�w��\\u001f&��dףF��Qbx��\\u0000�f�˲z�Ӳ��mY�\\u0016fY���a��ی�em}-mXM��m;�w�����M\\f�Z��\\\\���5.m\\t�\\\\�2�Ҋ�%��b�\\n[�[�-�Ao��\\u0004��(\\u00156KxX/\\u000b��,��^��d}/�.!Q\\u001f�S\\u0017\\u001b����6}\\u000f�.2*Z\\u001b\\u0016\\u001e�\\r\\u000e\\t�\\u0006��Z����hu\\u001aQ��(����x\\r�\\u0013\\u001a��U�x\\u0014�\\n�ö��\\u001e�a4��\\u001e�\\u0018�D�c�I��| D�\\u0013,�����\\u0012,�)X\\nݤ=�\\u0000P���P�\\n�r�ݎ\\u0002TQc�S\\u001d\\u0005�b�Ē]��T����J\\u001c��vn%�b��&L,�O�i���Q�!TJ��xcdw���\\u0011�^UPT��.mO��[�K��GcScc��\\u001f�]!����9����T��-ZS��\x7FG����H�ы�� V�\\u001d��\\\\t��YNo�[s�DǤ\x7F$��܅#l�\\u0003\\u0014fP\\u0001���҅��\\u0010�`�w�g2~�\\u0015��|����!\\u0004��/\\u00079�$�\x7F� 7�夕\\u0014�&�#ͨ?=�,K\\u0011�a�\\u001e\\u001e�\\u000b�\\u0007�\\u0012+\\t\':B��\\u0018Qs�#q$��P!�0N�g=+��*�Y�\\u0017~adn�\\u001a�����$z�\\u0012��\\u0011<��\\u0013�;l�\\u0007�I��P8��Ga��a\\u0017\\u001c�2/�3\\u001f�W�#\\u0011�\\u0004\\\\\\u001fV�\\r��\\u000ea&`�0�KV1#�s\\\\,(�\\\\&�Lc\\u000f��DA�q5;\\b��\\u0007l;��mp�Mf��<\\u0018\\u0001�I\\u001f�a�c{�\\u0016�\\u0018sߕ�!�A�,\\u0001;�c�����r��Jr ����|��\\u001b�;i\'��H˥��_:.ȁ�[�)\\u0014�I��R�S�\\u0007+`��e���C�I��z�\\"q\\u0004�$\\nǀ��8\\u0006��q�u�<ZF\\"�\\u0004��\\u0011y�D�!�#g�`y\x7F�W�>P�G8\\u0001_�3\\u001a��#�����\\u001b`�\\u0011�\\u0004\\u0005�6P�y���^��>~M�\\u0014��h3�!\\u0010,�W+\\u000f��\\u001dZ�v\\b\\u0003;�\\u00178��,?��+�,ð\\u0004^׳嬗Ń{\\u001d�\\u0006e��E�)��Gdʜe��xA��\\u0019����\\"�]>9M�����Ka��Ì�\\u0017�\\u0003xJ�b�`\\u000b��ߓ4�\\u00140\\u0005����d����3�\\n���0-�\\u0016�N\\t$R�S�Z��=\\u001eb8K�qCv6�QFl��\\u0010�a\\u000bV(�\\n&�\\u001c\\u0014\\u001a\\u0012\\u0012��Vt\\u001c8�A�Y�ݿ`����\\n����G$\\u0012Ϗ>�\\u001ev`�\\\\��\\u000fI�-���\\u0010J��H��.z�P�{Ly�B��TΔ��r���^�׬\\u000b\\u0002.\\u0012/�q�\\u000e�\\u001bj��\\u0012z\\f�Җ����\\u001e�և�\\u0019\\u001f�3�\\u0016\\u0012�n>�!�(��oం���ǐ�\\u001d\\u0007��җR�g�C:�.\\"!\\u000fm\'1s\\u0017\\u001d\\u0018\\u0016���6\\u001d�ʄp\'�\\u001a#��̞*�\\u001a\\u0019A\\u0010�aB�1QH4�fF��\\u0004�ڣ\\u001bf��L�K�1�1�\\u0012�4�:�\\\\]���7�bf1߬�/6�\\u001bu\\u000b\\f+\\r6�P\\u0000��\\u0018N\x7FBET�J\\u0003`4|�Uӽ\\u0011�Bۮ\\u0011���h(i�eg��\\f�C�\\fx�\\u0001���?�\\u0014W\\u0019vQ\\u0019g����qq\\u0019<�f�By�d�ԡ~[���#�ҝ3I��-�\\f��ng���#�J&�y���;�&\\u0013�����\\u0006�o\\u0016�`k�x��(����\\u0018����1N~4������4�\\u0013哭��s��X�D\\u0018��\\u001e8[�8�\\u001f�U0��\\u0012\\u0018r\\u001d&�*�[��\\fE��#����(V�)�(�(f+U��\\u0003�ދ�\\u0006�H8��_� \\n�\'\\f��.�D^\\u0005<��~ߧ� �!��!�^���ECxg��]�t8ʂB���Mo(�YY�\x7F\\u0010G�H~1,湲RR������!�Ёُe�#E\\u0006��Ge�$65��!5N�|o����\'\\u0007\\u001d���d�8\x7F�|\\u001d\\u0013��\\u001d��i�/�Y�\\u0019p��\\u0003��\\u0019�\\u0011��\x7F7��=�\\u0019\'j2�\\u0003\\u0011��h;\\u001b�%\\nN�\\u0019m��2��xUi�5���\\u0002Q�4e��M-&�d�X��ɮ��do2��\\u001c�\\u0016L���i�6:mr\\u001ak]���\\u0007gV��TȦ\\u0002-\\u0003���A\\u001c\\u000e>&�g<��\'(#=֝\\u001aB��ю�\\u001d\\u001fo��i�\\b6�4w*�}z��H����|��uuu���\\nb�9i�\'�.1)j\\\\zF�1�\\u0006�\\u001b6���[�\\r^�7�82!sj���)\\u0015\\u0015$��.b�V93�hr��Ma9Vk��_֡�7\\u001cJ�p����\\t��\\bO2\\u0004��4��T\\f#i�A4��^�A�\\"����9�|�\\"d����S�\\u0017�`\\u0001\\u0016�o�X<�B\\u000b\\u001bڢ|�\\u0007��2�YTV��٧�P�\\u0019������r�!A|\\u001f�kv��G�\\u0001G��I�ww��D�%\x84�wI�3��-\\\\TQ��u#�\\u0014��7�}%9}�c\\u00032\'IO���X����׆���DG\\u0013��~A*�0�\\u0013!�u)<�r�\\u0017��v\\u0005\\u001e�_˰��D�F��\\u0010�!p��h\\u0015b\\u000by\\\\c��3Ո���tȤ\\u0006Q�C�Muw��~9�\\n��9���s!��?&��|x����ز\\b9\\u001e3�օ\\u000eq9��\\\\I�\\n��e��i\\u0014�\\u0019\\u0000j�z��ax䎊����t��(�]��\\u000bM~�Y�,�^Z�9�]��҉�6l���g�\\u0017(��p�\\u0001���U��w�\\u0013��j�\\u001dJG�=�$�8j*_���/V.\\b�\\u001d��Y���\\u0019;%&K4d��\\"��fk�-ZN�5�\\u0013�����i\\u001f����jmK\\u0000�=�$b1�\\bV�)Xž8�oe�)�(�FJ1J�,�4���� �� [Q�\\u0001\\u001d��$\\u0018��3h��/���ۥ6i,�M��\\u0013ս����}���z\x7FN�04T $-Dz�yt\\\\ϑd+��\\u0013Ƀi��{�GFZ{�\\u001f����ߥ�\\u0018�Ēp��B\\\\���G?�\\u0013#r�\\u0015��]O\\u0004�ݪ\\u0016�g�\\u0006���VcѺ�\\u001e�W�����M5t�M\\u0005ggjvg�<�n�\\u001diC�d�8���Y���Y\\"�O\\u0011A��|v`�\\u0015I�\\u001d��b4�p2�\\u0011D\\rEcpW\\u0018\'\\u0002�;���/�I\\u0013F2�B\\u001dS-,d�tq�\\u000eI\\u0014�V\\u0006U�J��.ͨV�*�W\\u0011��<Z\\"ˌ\\u0001�\\t��\\u0016��\\u0016^\\u0016��,*2e�\\u0018Qh��\\\\a#\\r�wr\\u001d\\u0004��Ё*�j�rN\\u001d�N\\u0014��tu�8��\\u0013�s�*�13�l\\u0015.�ML�Z��*�f�D娉 Z��n�F��R\\u0000���Ũ�jƃ��Q��.�4~�\\u0004�6\\u0012�E2mee~�P\\u001e�\\bt �Tm;(����I��l6��ŝນ$%J�\\u0013�V�ʘ��΃Lq�\\u0014�\\u001f����9�k�������1#�֢�R\\u000e^��F��LV��1�W�����x�(V��?v��\\u000eŻX�\\u0006�=\\u0000*�\\u001b83\\rd\\u0014����Ҿ���<tf�#�\\u000e5\\u000b�F�W\\u001b�('... 702140 more characters
}
try to set in your response the correct mimetype: application/pdf
instead of using
response ={
statusCode:200,
body: doc
}
use this one:
response ={
statusCode:200,
headers: {"content-type": "application/pdf"},
body: doc
}
This should give your browser the information, of the correct content type.
Edit:
if this won't work, you probably need to follow this instructions: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings.html

InvalidClientQueryException when updating Document Library Metadata SPFx webpart

Need your help!
Am Uploading a document to the document library in SPFx webpart using REST API. I am not using PNP and React for file upload and updating metadata.
Post uploading the document, I am trying to update the metadata properties by getting the ID and updating the item.
File upload is done successfully. Able to get the ID of the Item added. While updating the metadata property - it is giving me Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"A type named 'SP.Data.SubmitxyzfileListItem' could not be resolved by the model. When a model is available, each type name must resolve to a valid type."}
Below is the code I am using
//Code for File Upload
private uploadFile(FinalName:string): Promise<any>{
try {
return new Promise((resolve) => {
var files = (<HTMLInputElement>document.getElementById('userFile')).files;
const spOpts: ISPHttpClientOptions = {
body: files[0]
};
var url=`${this.context.pageContext.web.absoluteUrl}${this.properties.primarylist}/_api/Web/Lists/getByTitle('${this.properties.fileuploadlist}')/RootFolder/Files/Add(url='${FinalName}', overwrite=true)`
const response = this.context.spHttpClient.post(url, SPHttpClient.configurations.v1, spOpts).then((response: SPHttpClientResponse) => {
response.json().then(async (responseJSON: any) => {
console.log("File Uploaded");
var uniqueGuid = await responseJSON.UniqueId;
this.updateDocLibMetadata(uniqueGuid);
console.log("uniqueGuid"+uniqueGuid);
resolve(uniqueGuid);
});
});
});
} catch (error) {
console.log("Error in uploadFile " + error);
}
}
\\Code for updating Document Library Metadata
private updateDocLibMetadata(uniqueGuid:string) {
var geturl=`${this.context.pageContext.web.absoluteUrl}${this.properties.primarylist}/_api/Web/Lists/getByTitle('${this.properties.fileuploadlist}')/GetItemByUniqueId(guid'${uniqueGuid}')?$select=id,name,UniqueId,Title,File/ServerRelativeUrl&$expand=File`;
this.context.spHttpClient.get(geturl,SPHttpClient.configurations.v1)
.then((response: SPHttpClientResponse) => {
response.json().then((responseJSON: any) => {
console.log("Id in updateDocLibMetadata :"+responseJSON["Id"]);
var itemId=responseJSON["Id"];
var posturl = this.context.pageContext.web.absoluteUrl +this.properties.primarylist+ `/_api/web/lists/GetByTitle('${this.properties.fileuploadlist}')/items(${itemId})`;
var payload = JSON.stringify({
"__metadata": {
'type': this.getListItemType(this.properties.fileuploadlist)
},
"Id_SubmitxyzQuestionId": this._requiredListProps.Id
});
var option = {
headers: {
'IF-MATCH': '*',
'Content-type': 'application/json;odata=verbose',
"accept": "application/json;odata=verbose",
"odata-version":"3.0",
'X-HTTP-Method': 'PATCH'
},
body: payload
};
//THIS FINAL CALL IS GIVING ERROR
return this.context.spHttpClient.post(posturl, SPHttpClient.configurations.v1, option).then((UpdateResponse: SPHttpClientResponse) => {
console.log(UpdateResponse.status + ':' + response.ok);
UpdateResponse.text().then(function (text) {
// do something with the text response
console.log("text:"+text);
});
})
.catch(reject => console.error('Error :', reject));
});
});
}
I did come through one post of PowerAutomate which says the solution was to check out the file then update the properties. Link to the PowerAutomate issue - https://powerusers.microsoft.com/t5/Building-Flows/Update-list-item-via-SharePoint-REST-API/m-p/534538#M69186
For SPFx webart also - would I have to use same approach ?
Any help is much appreciated.
Thanks.
Found answer to the above!
Changed the payload and the headers for the request and worked like a charm.
Changed to below code in updateDocLibMetadata method specified in the query.
var payload = JSON.stringify({
"Id_SubmitxyzQuestionId": this._requiredListProps.Id
});
var option = {
headers: {
'Accept': 'application/json;odata=nometadata',
'Content-type': 'application/json;odata=nometadata',
'odata-version': '',
'IF-MATCH': '*',
'X-HTTP-Method': 'MERGE'
},
body: payload
};

Make a HTTP Post call within Azure Functions using Nodejs

I am trying to make a HTTP Post call from within Azure Functions using nodejs. The payload which needs to be passed is in JSON with two properties title and description. For some reason, the below code doesn't work and I don't get any errors in the log too. The POST operation works in a Postman rest client. Can someone point me in the right direction? Thanks
var http = require('https');
module.exports = function (context, req) {
var body = {
"title": "Sunday",
"description": "Last day of the week"
}
const options = {
hostname: 'capservice-xxxxxxxx.ondemand.com',
port: 443,
path: '/incident/SafetyIncidents',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': body.length
}
}
var response = '';
const request = http.request(options, (res) => {
context.log(`statusCode: ${res.statusCode}`)
res.on('data', (d) => {
response += d;
})
res.on('end', (d) => {
context.res = {
body: response
}
context.done();
})
})
request.on('error', (error) => {
context.log.error(error)
context.done();
})
request.write(body);
request.end();
};
Had a bit more chance to investigate the issue. I've actually tested it on Azure Functions now - and got it to work.
Your body variable is incorrectly typed:
// This assumes that the body type is JSON.
var body = {
"title": "Sunday",
"description": "Last day of the week"
}
See the reference here: https://nodejs.dev/learn/make-an-http-post-request-using-nodejs
const https = require('https')
// See the JSON.stringify
const data = JSON.stringify({
todo: 'Buy the milk'
})
When I try to execute your function above as it is (with changing the URL) - I'm receiving a parameter error as below
FailureException: TypeError [ERR_INVALID_ARG_TYPE]: The first argument
must be of type string or an instance of Buffer. Received an instance
of ObjectStack
Adding the JSON.stringify to the body object solved the problem for me.

Unable to send mail using gmail api node js

I have followed gmail api for sending email. I am getting error as:
"message": "400 - \"{\n \\"error\\": {\n \\"errors\\": [\n {\n \\"domain\\": \\"global\\",\n \\"reason\\": \\"invalidArgument\\",\n \\"message\\": \\"'raw' RFC822 payload message string or uploading message via /upload/* URL required\\"\n }\n ],\n \\"code\\": 400,\n \\"message\\": \\"'raw' RFC822 payload message string or uploading message via /upload/* URL required\\"\n }\n}\n\""
Here is the piece of code I have written for sending mail using gmail api with node.js. Help me out to resolve the issue.
router.post('/composeMail', async (req, res, next) => {
function makeBody(to, from, subject, message) {
let str = ["Content-Type: text/plain; charset=\"UTF-8\"\n",
"Content-length: 5000\n",
"Content-Transfer-Encoding: message/rfc822\n",
"to: ", to,"\n",
"from: ", from,"\n",
"subject: ", subject,"\n\n",
message
].join('');
console.log("String: ", str);
// let encodedMail = new Buffer(str).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
let encodedMail = btoa(str).replace(/\+/g, '-').replace(/\//g, '_');
return encodedMail;
}
let raw = makeBody("dinesh.kumar#gmail.com", "dinesh.kumar#gmail.com", "Test mail", "Everything is fine");
let obj = {};
obj.raw = raw;
let body = JSON.stringify(obj);
let option = {
url: "https://www.googleapis.com/gmail/v1/users/userId/messages/send",
method: 'POST',
headers: {
'Authorization': `Bearer ${req.query.access_token}`
},
qs: {
userId: 'me'
},
body: body
};
await request(option).then(body => {
return res.apiOk(body);
}).catch(err => {
return res.apiError(err);
})
});
You want to send an email using Gmail API by the request module.
If my understanding is correct, how about this modification? I think that there are several answers. So please think of this as one of them.
Modification points:
Please use https://www.googleapis.com/upload/gmail/v1/users/userId/messages/send as the endpoint.
Use the value as a string.
Add 'Content-Type': 'message/rfc822' to the headers.
Modified script:
Please modify makeBody() as follows.
function makeBody(to, from, subject, message) {
let str = [
"to: ", to, "\n",
"from: ", from, "\n",
"subject: ", subject, "\n\n",
message,
].join('');
return str;
}
Please modify option as follows.
let raw = makeBody("dinesh.kumar#gmail.com", "dinesh.kumar#gmail.com", "Test mail", "Everything is fine");
const userId = 'me'; // Please modify this for your situation.
let option = {
url: "https://www.googleapis.com/upload/gmail/v1/users/" + userId + "/messages/send",
method: 'POST',
headers: {
'Authorization': `Bearer ${req.query.access_token}`,
'Content-Type': 'message/rfc822',
},
body: raw,
};
Note:
This modified script supposes that Gmail API is enabled at API console and the required scope for sending emails is included in the scopes of access token.
Reference:
Users.messages: send
In my environment, I could confirm that this modified script worked fine. But if this was not what you want, I'm sorry.

How to use facebook messenger send image API in Node js?

I'm building a facebook bot in nodejs with facebook messenger API. I'm trying to send a image from the bot by directly uploading the image file from the heroku server itself (not through URL) and it does not work.
Here is a error log from the console.
Failed calling Send API 400 Bad Request { message: '(#100) Incorrect
number of files uploaded. Must upload exactly one file.',type:
'OAuthException', code: 100,error_subcode: 2018005,fbtrace_id:
'E32ogm/ofxd' }
The official facebook document only contains an example in curl format and I'dont know how to replicate this curl into node format.
I've tested with curl and it worked like a charm.
curl \ -F 'recipient={"id":"recipientId"}' \ -F 'message={"attachment":{"type":"image", "payload":{}}}' \ -F 'filedata=#resource/pdf_img/sample.jpg;type=image/jpeg' \ "https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN"
This is my node implementation that seems to be problematic,
//file_loc = __dirname+"/resource/pdf_img/sample.jpg"
function sendImageMessage(recipientId, file_loc){
let fs = require('fs');
var readStream = fs.createReadStream(file_loc);
var messageData = {
recipient : {
id : recipientId
},
message : {
attachment : {
type : "image",
payload :{}
}
},
filedata:readStream
}
callSendAPI(messageData);
}
function callSendAPI(messageData) {
request({
uri: "https://graph.facebook.com/v2.6/me/messages",
qs: {access_token: process.env.PAGE_ACCESS_TOKEN},
method: "POST",
json: messageData
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
var recipientId = body.recipient_id;
var messageId = body.message_id;
if (messageId) {
console.log("Successfully sent message with id %s to recipient %s",
messageId, recipientId);
} else {
console.log("Successfully called Send API for recipient %s",
recipientId);
}
} else {
console.error("Failed calling Send API", response.statusCode, response.statusMessage, body.error);
}
});
}
Please any help with fixing my node implementation or translating that curl in to node would be appreciated.
You can use forms /form-data/ in the request module (which has integrated module 'form-data').
But all requests needs to be stringified.
So, based on your example, the following should do the job >>
function sendImageMessage(recipientId, file_loc){
let fs = require('fs');
var readStream = fs.createReadStream(file_loc);
var messageData = {
recipient : {
id : recipientId
},
message : {
attachment : {
type : "image",
payload :{}
}
},
filedata:readStream
}
callSendAPI(messageData);
}
function callSendAPI(messageData) {
var endpoint = "https://graph.facebook.com/v2.6/me/messages?access_token=" + process.env.PAGE_ACCESS_TOKEN;
var r = request.post(endpoint, function(err, httpResponse, body) {
if (err) {return console.error("upload failed >> \n", err)};
console.log("upload successfull >> \n", body); //facebook always return 'ok' message, so you need to read error in 'body.error' if any
});
var form = r.form();
form.append('recipient', JSON.stringify(messageData.recipient));
form.append('message', JSON.stringify(messageData.message));
form.append('filedata', messageData.filedata); //no need to stringify!
}
Details here https://github.com/request/request#forms
I think sending variable messagedata as formdata would solve the problem.
var FormData = require('form-data');
var fs = require('fs');
var https = require('https');
function sendImageMessage(recipientId, file_loc){
var readStream = fs.createReadStream(file_loc);
var messageData = new FormData();
messageData.append('recipient', '{id:' +recipientId+ '}');
messageData.append('message', '{attachment :{type:"image", payload:{}}}');
messageData.append('filedata', readStream);
callSendAPI(messageData);
}
Secondly you need to change request a bit since now you are using formdata. I have done it using module https and so have changed the callSendAPI() code accordingly. You can find out how to send the formdata using request module.
function callSendAPI(messageData) {
var options = {
method: 'post',
host: 'graph.facebook.com',
path: '/v2.6/me/messages?access_token=' + pagetoken,
headers: messageData.getHeaders()
};
var request = https.request(options);
messageData.pipe(request);
request.on('error', function(error) {
console.log("Unable to send message to recipient %s", recipientId);
return;
});
request.on('response', function(res) {
if (res.statusMessage == "OK") {
console.log("Successfully sent message to recipient %s", recipientId);
} else {
console.log("Unable to send message to recipient %s", recipientId);
}
return;
});
}

Resources