英文:
TypError when calling an API from a lambda function
问题
I need to call an API from my lambda function to send an OTP and for that for the path I need to pass some query strings so I do as shown below.
const https = require('https');
function postRequest(body) {
const ans = body.answer
const phon = body.phone
const options = {
hostname: 'app.xxx.xx',
path: '/api/v1/send?user_id=24xxx&api_key=3Yxxxx&sender_id=dEMO&to=' + phon + '&message=Your%OTP%is%' + ans + 'thanks',
method: 'POST',
port: 443,
};
return new Promise((resolve, reject) => {
const req = https.request(options, res => {
let rawData = '';
res.on('data', chunk => {
rawData += chunk;
});
res.on('end', () => {
try {
resolve(JSON.parse(rawData));
} catch (err) {
reject(new Error(err));
}
});
});
req.on('error', err => {
reject(new Error(err));
});
req.write(JSON.stringify(body));
req.end();
});
}
exports.handler = async (event, context, callback) => {
// Create a random number for OTP
const challengeAnswer = Math.random().toString(10).substr(2, 4);
const phoneNumber = event.request.userAttributes.phone_number;
console.log(event, context);
await postRequest({
phone: phoneNumber,
answer: challengeAnswer,
},
function(err, data) {
if (err) {
console.log(err.stack);
console.log(data);
return;
}
console.log(`SMS sent to ${phoneNumber} and OTP = ${challengeAnswer}`);
return data;
});
callback(null, event);
};
But when I do so I get this error,
'TypeError [ERR_UNESCAPED_CHARACTERS]: Request path contains unescaped characters'
How to fix this?
英文:
I need to call an API from my lambda function to send an OTP and for that for the path I need to pass some query strings so I do as shown below.
const https = require('https');
function postRequest(body) {
const ans = body.answer
const phon = body.phone
const options = {
hostname: 'app.xxx.xx',
path: '/api/v1/send?user_id=24xxx&api_key=3Yxxxx&sender_id=dEMO&to='+{phon}+'&message=Your%OTP%is%'+{ans}+'thanks',
method: 'POST',
port: 443,
};
return new Promise((resolve, reject) => {
const req = https.request(options, res => {
let rawData = '';
res.on('data', chunk => {
rawData += chunk;
});
res.on('end', () => {
try {
resolve(JSON.parse(rawData));
} catch (err) {
reject(new Error(err));
}
});
});
req.on('error', err => {
reject(new Error(err));
});
req.write(JSON.stringify(body));
req.end();
});
}
exports.handler = async (event, context, callback) => {
//Create a random number for otp
const challengeAnswer = Math.random().toString(10).substr(2, 4);
const phoneNumber = event.request.userAttributes.phone_number;
console.log(event, context);
await postRequest({
phone: phoneNumber,
answer: challengeAnswer,
},
function(err, data) {
if (err) {
console.log(err.stack);
console.log(data);
return;
}
console.log(`SMS sent to ${phoneNumber} and otp = ${challengeAnswer}`);
return data;
});
callback(null, event);
};
But when I do so I get this error,
'TypeError [ERR_UNESCAPED_CHARACTERS]: Request path contains unescaped characters'
How to fix this?
答案1
得分: 1
Here is the translated code snippet:
你可以尝试将以下代码部分替换为安全的 URL 格式:
```js
const { phone, answer } = body;
const options = {
hostname: 'app.xxx.xx',
path: encodeURI(`/api/v1/send?user_id=24xxx&api_key=3Yxxxx&sender_id=dEMO&to=${phone}&message=Your%OTP%is%${answer}thanks`),
method: 'POST',
port: 443,
};
你也可以尝试对整个 options
对象进行 JSON.stringify()
处理,但需要确保你的 API 能够接受 JSON 格式的数据。
<details>
<summary>英文:</summary>
You may try to replace:
```js
const ans = body.answer
const phon = body.phone
const options = {
hostname: 'app.xxx.xx',
path: '/api/v1/send?user_id=24xxx&api_key=3Yxxxx&sender_id=dEMO&to='+{phon}+'&message=Your%OTP%is%'+{ans}+'thanks',
method: 'POST',
port: 443,
};
By:
const { phone, answer } = body;
const options = {
hostname: 'app.xxx.xx',
path: encodeURI(`/api/v1/send?user_id=24xxx&api_key=3Yxxxx&sender_id=dEMO&to=${phone}&message=Your%OTP%is%${answer}thanks`),
method: 'POST',
port: 443,
};
in order to have an url safe format.
You may also try to JSON.stringify()
the whole options
object, but it will need to ensure your API can consume JSON.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论