Skip to content

Substitutions must be strings, else 400 Bad Request when sending #282

@rfunduk

Description

@rfunduk

Issue Summary

Trying to send an email using a template + substitutions that include an integer gives a 400 Bad Request error with no explanation.

Steps to Reproduce

  1. Create a template with a substitution (eg %test%)
  2. Attempt to send that template using sendgrid-node where the substitution is an integer

Expected - Email is sent, or error with info about the problem.

Actual - 400 Bad Request with no details as to the problem.

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' } }

Here is a code example:

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 )
} )

The exact same code except with '42' instead of 42 for the substitution works.

While it would be super great if SendGrid itself would not error like this (or would explain the problem in the error message, like which field and that it wasn't accepted because it's a number), I think sendgrid-node could/should easily handle it by just toString()-ing substitutions.

Happy to submit a PR, but when I went to go and write a failing test there are no docs on how to run the tests, and in the tests I saw a lot of stuff about test hosts that I wouldn't have running.

Technical details:

  • sendgrid-nodejs Version: 2.2.1
  • Node.js Version: 4.1.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions