TypeError 在从 Lambda 函数调用 API 时发生。

huangapple go评论117阅读模式
英文:

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.

  1. const https = require('https');
  2. function postRequest(body) {
  3. const ans = body.answer
  4. const phon = body.phone
  5. const options = {
  6. hostname: 'app.xxx.xx',
  7. path: '/api/v1/send?user_id=24xxx&api_key=3Yxxxx&sender_id=dEMO&to=' + phon + '&message=Your%OTP%is%' + ans + 'thanks',
  8. method: 'POST',
  9. port: 443,
  10. };
  11. return new Promise((resolve, reject) => {
  12. const req = https.request(options, res => {
  13. let rawData = '';
  14. res.on('data', chunk => {
  15. rawData += chunk;
  16. });
  17. res.on('end', () => {
  18. try {
  19. resolve(JSON.parse(rawData));
  20. } catch (err) {
  21. reject(new Error(err));
  22. }
  23. });
  24. });
  25. req.on('error', err => {
  26. reject(new Error(err));
  27. });
  28. req.write(JSON.stringify(body));
  29. req.end();
  30. });
  31. }
  32. exports.handler = async (event, context, callback) => {
  33. // Create a random number for OTP
  34. const challengeAnswer = Math.random().toString(10).substr(2, 4);
  35. const phoneNumber = event.request.userAttributes.phone_number;
  36. console.log(event, context);
  37. await postRequest({
  38. phone: phoneNumber,
  39. answer: challengeAnswer,
  40. },
  41. function(err, data) {
  42. if (err) {
  43. console.log(err.stack);
  44. console.log(data);
  45. return;
  46. }
  47. console.log(`SMS sent to ${phoneNumber} and OTP = ${challengeAnswer}`);
  48. return data;
  49. });
  50. callback(null, event);
  51. };

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.

  1. const https = require('https');
  2. function postRequest(body) {
  3. const ans = body.answer
  4. const phon = body.phone
  5. const options = {
  6. hostname: 'app.xxx.xx',
  7. path: '/api/v1/send?user_id=24xxx&api_key=3Yxxxx&sender_id=dEMO&to='+{phon}+'&message=Your%OTP%is%'+{ans}+'thanks',
  8. method: 'POST',
  9. port: 443,
  10. };
  11. return new Promise((resolve, reject) => {
  12. const req = https.request(options, res => {
  13. let rawData = '';
  14. res.on('data', chunk => {
  15. rawData += chunk;
  16. });
  17. res.on('end', () => {
  18. try {
  19. resolve(JSON.parse(rawData));
  20. } catch (err) {
  21. reject(new Error(err));
  22. }
  23. });
  24. });
  25. req.on('error', err => {
  26. reject(new Error(err));
  27. });
  28. req.write(JSON.stringify(body));
  29. req.end();
  30. });
  31. }
  32. exports.handler = async (event, context, callback) => {
  33. //Create a random number for otp
  34. const challengeAnswer = Math.random().toString(10).substr(2, 4);
  35. const phoneNumber = event.request.userAttributes.phone_number;
  36. console.log(event, context);
  37. await postRequest({
  38. phone: phoneNumber,
  39. answer: challengeAnswer,
  40. },
  41. function(err, data) {
  42. if (err) {
  43. console.log(err.stack);
  44. console.log(data);
  45. return;
  46. }
  47. console.log(`SMS sent to ${phoneNumber} and otp = ${challengeAnswer}`);
  48. return data;
  49. });
  50. callback(null, event);
  51. };

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:

  1. 你可以尝试将以下代码部分替换为安全的 URL 格式
  2. ```js
  3. const { phone, answer } = body;
  4. const options = {
  5. hostname: 'app.xxx.xx',
  6. path: encodeURI(`/api/v1/send?user_id=24xxx&api_key=3Yxxxx&sender_id=dEMO&to=${phone}&message=Your%OTP%is%${answer}thanks`),
  7. method: 'POST',
  8. port: 443,
  9. };

你也可以尝试对整个 options 对象进行 JSON.stringify() 处理,但需要确保你的 API 能够接受 JSON 格式的数据。

  1. <details>
  2. <summary>英文:</summary>
  3. You may try to replace:
  4. ```js
  5. const ans = body.answer
  6. const phon = body.phone
  7. const options = {
  8. hostname: &#39;app.xxx.xx&#39;,
  9. path: &#39;/api/v1/send?user_id=24xxx&amp;api_key=3Yxxxx&amp;sender_id=dEMO&amp;to=&#39;+{phon}+&#39;&amp;message=Your%OTP%is%&#39;+{ans}+&#39;thanks&#39;,
  10. method: &#39;POST&#39;,
  11. port: 443,
  12. };

By:

  1. const { phone, answer } = body;
  2. const options = {
  3. hostname: &#39;app.xxx.xx&#39;,
  4. path: encodeURI(`/api/v1/send?user_id=24xxx&amp;api_key=3Yxxxx&amp;sender_id=dEMO&amp;to=${phone}&amp;message=Your%OTP%is%${answer}thanks`),
  5. method: &#39;POST&#39;,
  6. port: 443,
  7. };

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.

huangapple
  • 本文由 发表于 2023年1月3日 17:16:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/74991236.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定