Can't send emails with the new v3 Sendgrid API using NodeJs - javascript

I'm trying to send an email but it isn't going through.
I used your example on README and also used the non Helper method one but none of them seems to work.
import { mail } from 'sendgrid';
const helper = mail;
const from_email = new helper.Email('admin#test.com');
const to_email = new helper.Email('someuser#test.com');
const subject = 'Testing';
const content = new helper.Content('text/plain', 'Test Description');
const email = new helper.Mail(from_email, subject, to_email, content);
const sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
const request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: email.toJSON()
});
sg.API(request, function(error, response) {
console.log(response.statusCode);
console.log(response.body);
console.log(response.headers);
});
The first console.log spits out the statusCode of 202.
The second console.log spits out nothing.
The last console.log spits out this:
{
server: 'nginx',
date: 'Mon, 15 Aug 2016 08:59:42 GMT',
'content-type': 'text/plain; charset=utf-8',
'content-length': '0',
connection: 'close',
'x-message-id': 'kQdayBbvSKSb9ZlVDCUKTg',
'x-frame-options': 'DENY'
}

For anyone having similar issue.
My account was apparently deactivated as I hadn't logged in to answer their support query. So what you need to do is to contact their support team.

Related

How to sign API Gateway URL that has a query string

I am trying to use SignatureV4 to sign a request for an API Gateway endpoint that uses IAM Authorizor. An issue is that I keep getting a 403 error whenever I append a query string to my URL, i.e. /pets?type=1. Everything works fine when a query string is not included, i.e. /pets
This is how I build a request:
const region = 'xxx'
const method = 'GET'
const protocol = 'https:'
const host = `xxx.execute-api.${region}.amazonaws.com`
const path = '/dev/pets'
const query = {
type: 1,
}
const request = new HttpRequest({
method: method,
protocol: protocol,
hostname: host,
path: path,
query: query,
headers: {
'Content-Type': 'application/json',
host: host ,
},
})
const signer = new SignatureV4({
credentials: AWS.config.credentials,
service: 'execute-api',
region: region,
sha256: Sha256,
})
const { headers } = await signer.sign(request)
const response = await fetch(`${protocol}//${host}${path}?type=1`, {
headers,
method
}).then((res) => res.json())
I've tried running the same query within Postman and it worked just fine. So, I have to assume that an issue is with my implementation.
An issue was due to getCanonicalQuery ignoring values that are not string or array:
https://github.com/aws/aws-sdk-js-v3/blob/main/packages/signature-v4/src/getCanonicalQuery.ts#L19
So, to fix this I had to swap my query to below:
const query = {
type: '1',
}

Nock does not returns the setted headers

I'm using Nock to intercept a http request.
test.js:
const nock = require('nock');
const server = require('../server');
const request = require('request');
describe('My test', () =>{
it('Should returns the customized header', () => {
nock('url')
.get('/test')
.reply(302, {
'user-agent': 'Mozilla'
})
const { headers } = await request(server).get('/test');
expect(headers['user-agent']).to.include('Mozilla');
}
})
When I run the test, it fails and the log of headers received by request is like that:
{
'user-agent': 'node-superagent/3.8.3',
location: 'undefined',
vary: 'Accept, Accept-Encoding',
'content-type': 'text/plain; charset=utf-8',
'content-length': '31',
date: 'Fri, 24 May 2019 09:15:46 GMT',
connection: 'close'
}
Do I missed something or it's the normal behaviour of Nock?
The issue is with the way you're passing the headers to the reply function. Headers are the third argument for that method, but you're providing them as the second arg which means the object with the the user-agent key is being used as the body. Since it's a 302 and you probably want an empty body, you should pass an empty string as the second arg.
nock('url')
.get('/test')
.reply(302, '', {
'user-agent': 'Mozilla'
})
Related docs.

Invalid protocol: undefined - NodeJS error on POST request -

I'm getting this error coming from my require.post(error)
its a lambda function deployed from a vagrant box. It is a wrapper for an api, the event.body has the properly formatted post json and everything is working perfectly when the post is done from postman. I have seen a roughly similar problem solved by
npm config set proxy http://usr:pwd#host:port
npm config set https-proxy http://usr:pwd#host:port
Please help! : )
Error: Invalid protocol: undefined
at Request.init (/var/task/node_modules/request/request.js:454:31)
at new Request (/var/task/node_modules/request/request.js:127:8)
at request (/var/task/node_modules/request/index.js:53:10)
at Function.post (/var/task/node_modules/request/index.js:61:12)
at module.exports.startVerifyProcess (/var/task/handler.js:83:13)
my Code:
module.exports.startVerifyProcess = (event, context, callback) => {
var body = JSON.parse(event.body);
const params = querystring.parse(event.body);
console.warn(body);
var Re;
var post_options = {
host: 'api.demo.veri.com',
path: '/api/v1/verify/requests',
port: 443,
method: 'POST',
headers: {
// 'Content-Type': 'application/json',
// 'Content-Length': Buffer.byteLength(event.body),
"Authorization": "myValidAuth",
},
}
request.post(post_options, body, function(err, res, resBody) {
if (err){
console.error(err);
}
console.warn("RESPONSE " + resBody);
});
callback(null, {
statusCode: 200,
body: JSON.stringify({
body: Re
})
});
}
The issue here is that request should not take the body as the second parameter.
request.post(post_options, function(err, res, resBody) is correct and the body should be in the post_options object. While your at it chose either camelCase or snake_case for everything in that project. Also check out node-fetch I would consider it a solid upgrade from using request.

get call status in nodejs from grandstream phone

I am trying to fetch the call status of a grandstream phone by a nodejs script. But I've run in some trouble. The first request is going all fine, and returning that I am authenticated. The second request isn't going well, it says that I'm not authenticated.
How do I set the credentials or the cookie from the first request in the second request, so it knows that I'm loggedin?
First request response:
Response=Success
Message=Authentication accepted
Needchange=0
Ver=1.0.3.92
First request response headers:
{
'status': '200',
'content-length': '79',
'content-location': 'http://192.168.0.1/manager?action=login&username=XXXXXX&secret=XXXXXX',
'set-cookie': 'phonecookie="XXXXXX";HttpOnly, type=admin;, Version="1";, Max-Age=900',
'server': 'Enterprise Phone',
'pragma': 'no-cache',
'cache-control': 'no-cache',
'date': 'Wed, 14 Jun 2017 10:22:29 GMT',
'content-type': 'text/plain'
}
Second request response:
Response=Error
Message=Authentication Required
App.js Script:
var fetch = require('node-fetch');
var host = '192.168.0.1';
var loginUrl = "/manager?action=login&username=XXXXXX&secret=XXXXXX";
var statusUrl = "/manager?action=lineStatus&line=0";
function makeRequest(url)
{
fetch("http://" + host + loginUrl).then(function(resultLogin) {
var resultAuth = result.body();
fetch("http://" + host + statusUrl, {method: 'GET').then(function(resultStatus) {
var resultStatus = resultStatus.body();
});
});
}
makeRequest();
Use fetch-cookie to let node-fetch store and send back cookies according to the url.
var fetch = require('fetch-cookie')(require('node-fetch'))

Verifiy iOS Receipt with Node.js

After struggling a few days trying to get something to work and getting no where, I was wondering if someone has gotten iOS Receipt Validation working on Node.js. I have tried the node module iap_verifier found here but I could not get it to work properly for me. the only response I received back form Apples servers is 21002, data was malformed.
One thing that has worked for me was a client side validation request to apples servers that I got directly from the tutorials provided by Apple here, with the code shown below.
// The transaction looks ok, so start the verify process.
// Encode the receiptData for the itms receipt verification POST request.
NSString *jsonObjectString = [self encodeBase64:(uint8_t *)transaction.transactionReceipt.bytes
length:transaction.transactionReceipt.length];
// Create the POST request payload.
NSString *payload = [NSString stringWithFormat:#"{\"receipt-data\" : \"%#\", \"password\" : \"%#\"}",
jsonObjectString, ITC_CONTENT_PROVIDER_SHARED_SECRET];
NSData *payloadData = [payload dataUsingEncoding:NSUTF8StringEncoding];
// Use ITMS_SANDBOX_VERIFY_RECEIPT_URL while testing against the sandbox.
NSString *serverURL = ITMS_SANDBOX_VERIFY_RECEIPT_URL;
// Create the POST request to the server.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serverURL]];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:payloadData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[conn start];
I have a bunch of different code I have been using to send a wide array of things to my node server. and all of my different attempts have failed. I have even tried just funneling the "payloadData" I constructed in the client side validation example above to my server and sending that to Apples servers with the following code:
function verifyReceipt(receiptData, responder)
{
var options = {
host: 'sandbox.itunes.apple.com',
port: 443,
path: '/verifyReceipt',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(receiptData)
}
};
var req = https.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log("body: " + chunk);
});
});
req.write(receiptData);
req.end();
}
Where the function is passed the payloadData. The response received from Apple is always 21002. I'm still basically a node novice,so I can't figure out what exactly is going wrong. I think there might be some data corruption happening when I am sending the data from ObjC to my Node server, so perhaps I am not transmitting right.
If anyone can point me in the right direction, or provide some example of how they got receipt validation to work in node for them, it would be a great help. It would be great if anyone has had any experience with the iap_verifier module, and exactly what data it requires. I'll provide any code example I need to, as I have been fighting this process for a few days now.
Thanks!
For anyone using the npm library "request", here's how to avoid that bothersome 21002 error.
formFields = {
'receipt-data': receiptData_64
'password': yourAppleSecret
}
verifyURL = 'https://buy.itunes.apple.com/verifyReceipt' // or 'https://sandbox.itunes.apple.com/verifyReceipt'
req = request.post({url: verifyURL, json: formFields}, function(err, res, body) {
console.log('Response:', body);
})
This is my working solution for auto-renewable subscriptions, using the npm request-promise library.
Without JSON stringify-ing the body form, I was receiving 21002 error (The data in the receipt-data property was malformed or missing)
const rp = require('request-promise');
var verifyURL = 'https://sandbox.itunes.apple.com/verifyReceipt';
// use 'https://buy.itunes.apple.com/verifyReceipt' for production
var options = {
uri: verifyURL,
method: 'POST',
headers: {
'User-Agent': 'Request-Promise',
'Content-Type': 'application/x-www-form-urlencoded',
},
json: true
};
options.form = JSON.stringify({
'receipt-data': receiptData,
'password': password
});
rp(options).then(function (resData) {
devLog.log(resData); // 0
}).catch(function (err) {
devLog.log(err);
});
Do you have composed correctly receiptData? Accordlying with Apple specification it should have the format
{"receipt-data": "your base64 receipt"}
Modifying your code wrapping the base64 receipt string with receipt-data object the validation should works
function (receiptData_base64, production, cb)
{
var url = production ? 'buy.itunes.apple.com' : 'sandbox.itunes.apple.com'
var receiptEnvelope = {
"receipt-data": receiptData_base64
};
var receiptEnvelopeStr = JSON.stringify(receiptEnvelope);
var options = {
host: url,
port: 443,
path: '/verifyReceipt',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(receiptEnvelopeStr)
}
};
var req = https.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log("body: " + chunk);
cb(true, chunk);
});
res.on('error', function (error) {
console.log("error: " + error);
cb(false, error);
});
});
req.write(receiptEnvelopeStr);
req.end();
}

Categories

Resources