Sendgrid-nodejs: 替代必须为字符串,否则为400错误请求(发送时)

创建于 2016-08-31  ·  13评论  ·  资料来源: sendgrid/sendgrid-nodejs

发行摘要

尝试使用包含整数的模板+替代来发送电子邮件会出现400 Bad Request错误,没有任何解释。

重现步骤

  1. 创建一个带有替换的模板(例如%test%
  2. 尝试使用sendgrid-node发送该模板,其中替换为整数

_Expected_-已发送电子邮件,或有关问题信息的错误。

_Actual_-400错误的请求,没有有关该问题的详细信息。

SENDMAIL ERROR { statusCode: 400,
  body: '{"errors":[{"message":"Bad Request","field":null,"help":null}]}',
  headers:
   { server: 'nginx',
     date: 'Wed, 31 Aug 2016 14:32:57 GMT',
     'content-type': 'application/json',
     'content-length': '63',
     connection: 'close',
     'x-frame-options': 'DENY' } }

这是一个代码示例:

var mail = new helper.Mail()
mail.setFrom( new helper.Email( '[email protected]' ) )
mail.setTemplateId( "THE_TEMPLATE_ID" )

var personalization = new helper.Personalization()
personalization.addTo( new helper.Email( '[email protected]' ) )
var sub = new helper.Substitution( `%test%`, 42 )
personalization.addSubstitution( sub )
mail.addPersonalization( personalization )

sgClient.API( sgClient.emptyRequest( {
  method: 'POST',
  path: '/v3/mail/send',
  body: mail.toJSON()
} ) ).catch( function( err ) {
  console.error( 'SENDGRID ERROR', err.response )
} )

完全相同的代码,但用'42'代替42代替替换。

如果SendGrid本身不这样出错(或在错误消息中说明问题,例如哪个字段,并且因为它是一个数字而未被接受),那将是非常好的,但我认为sendgrid-node可以/应该很容易仅通过toString()-ing替换来处理它。

提交PR很高兴,但是当我去编写失败的测试时,没有关于如何运行测试的文档,并且在测试中,我看到了很多我无法运行的有关测试主机的信息。

技术细节:

  • sendgrid-nodejs版本:2.2.1
  • Node.js版本:4.1.0
hacktoberfest help wanted community enhancement

最有用的评论

@wizysanjay在您的替换toString ,然后传递给库。 例如,代替:

personalization.addSubstitution( new helper.Substitution( `%number%`, number ) )

做这个:

personalization.addSubstitution( new helper.Substitution( `%number%`, number.toString() ) )

所有13条评论

@rfunduk

感谢您的详细反馈!

这是一个已知问题,API团队已对其积压进行了修复,以提供适当的错误字符串。

您是正确的,我们需要在库中对此进行修复,我们希望您提出请求。

我很高兴向您介绍测试的工作方式。 但首先,我们需要签署的CLA: https

谢谢!

我遇到了同样的问题。 我该如何解决

谢谢

@wizysanjay在您的替换toString ,然后传递给库。 例如,代替:

personalization.addSubstitution( new helper.Substitution( `%number%`, number ) )

做这个:

personalization.addSubstitution( new helper.Substitution( `%number%`, number.toString() ) )

+1

我知道了。 谢谢

我不敢相信我花了一个下午的调试时间才遇到这个问题。 至少在文档中提到substitution must be string会很好……我要加我自己,但是在这一点上,我什至找不到在文档中的位置。

@dangerismycat

:(不酷。这里记录了此内容,但这对您的用例没有帮助。

我们将其构建到库中,因此您甚至不必担心它。 您的投票已添加,以帮助提高优先级。

太好了,很高兴它提高了优先级。 如果这是一个简单的修复,并且您可以为我指明正确的方向(存储库或文件),则我可以解决。

@dangerismycat

太棒了! 解决方法可能会在这里

@thinkingserious Hm…看起来构建失败。 是否有我需要更新的相关测试或其他?

Travis CI版本对于那些不在SendGrid组织中的人员来说是不对的。 我不太确定发生了什么,但是我会在本地验证。

嘿,仍然在“ @ sendgrid / client”上出现此问题
当sub是整数时发送此消息不起作用,并且给出

body: {
  personalizations: [{
    to: [{
      email: userEmail
    }],
    subject: `blah blah blah`,
    substitutions: {
      '{{sub}}': sub
    }
  }],
  from: {
    email: '[email protected]',
    name: 'blah blah'
  },
  template_id
}
err { Error: Bad Request
    at Request.http [as _callback] (node_modules/@sendgrid/client/src/classes/client.js:124:25)
    at Request.self.callback (node_modules/request/request.js:186:22)
    at emitTwo (events.js:126:13)
    at Request.emit (events.js:214:7)
    at Request.<anonymous> (node_modules/request/request.js:1163:10)
    at emitOne (events.js:116:13)
    at Request.emit (events.js:211:7)
    at IncomingMessage.<anonymous> (node_modules/request/request.js:1085:12)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1055:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickDomainCallback (internal/process/next_tick.js:218:9)
  code: 400,
  message: 'Bad Request',
  response: 
   { headers: 
      { server: 'nginx',
        date: 'Fri, 20 Apr 2018 16:17:42 GMT',
        'content-type': 'application/json',
        'content-length': '63',
        connection: 'close',
        'access-control-allow-origin': 'https://sendgrid.api-docs.io',
        'access-control-allow-methods': 'POST',
        'access-control-allow-headers': 'Authorization, Content-Type, On-behalf-of, x-sg-elas-acl',
        'access-control-max-age': '600',
        'x-no-cors-reason': 'https://sendgrid.com/docs/Classroom/Basics/API/cors.html' },
     body: { errors: [Array] } } }
where the errors: array is [ { message: 'Bad Request', field: null, help: null } ]

但是,当我将请求中的sub更改为${sub} ,它将起作用

问候

你好@Mauyeung

您介意打开一个新期刊吗? 谢谢!

看来这可能是一个错误,因为认为我们应该在幕后将其转换为字符串。

最诚挚的问候,

埃尔默

此页面是否有帮助?
0 / 5 - 0 等级