Sendgrid-php: Error code 400: "Bad Request" when adding custom arguments as Integer values

Created on 24 Jan 2017  路  6Comments  路  Source: sendgrid/sendgrid-php

Issue Summary

The following response is received when trying to add a numeric custom parameter:

object(SendGrid\Response)[197]
  protected 'statusCode' => int 400
  protected 'body' => string '{"errors":[{"message":"Bad Request","field":null,"help":null}]}' (length=63)
  protected 'headers' => array
      0 => string 'HTTP/1.1 400 Bad Request'
      1 => string 'Server: nginx'
      2 => string 'Date: Tue, 24 Jan 2017 13:29:50 GMT'
      3 => string 'Content-Type: application/json'
      4 => string 'Content-Length: 63'
      5 => string 'Connection: keep-alive'
      6 => string 'X-Frame-Options: DENY'
      7 => string ''
      8 => string ''

JSON encoded request data looks as below:

{
  "from": {
    "email": "<correct email here>"
  },
  "personalizations": [
    {
      "to": [
        {
          "email": "<correct email here>"
        }
      ],
      "custom_args": {
        "param": 111
      }
    }
  ],
  "subject": "Subject",
  "content": [
    {
      "type": "text/html",
      "value": "<h1>hello</h1>"
    }
  ]
}

As shown above, json_encode doesn't quotes number properties. Thus 111 is sent as number.

However if the parameter is casted to (string) type, the email is sent successfully.

Steps to Reproduce

  1. Set up a simple email sending request with SendGrid php lib
  2. Add a custom numeric parameter via Personalization::addCustomArg() of the Personalization instance linked to the Mail
  3. Send the request

Technical details:

PHP 5.6.29 and 7.0.13
SendGrid 5.1.2

help wanted bug

All 6 comments

Thanks for taking the time to report this issue @igorokb. I've added this to our backlog so that we will always encode that parameter correctly behind the scenes.

It seems to me to have the same bug, is there a workaround ?
Tried this ($val replaced by $val."" to force to string) without success :

foreach($substitions as $var => $val) {
$personalization->addSubstitution($var, array($val.""));
}

@nelsounet,

Can you please post your payload?

$json_string = json_encode($mail, JSON_PRETTY_PRINT);
echo $json_string;

Thanks!

+1 ran into the same issue

compared to your example code (https://github.com/sendgrid/sendgrid-php/blob/master/USE_CASES.md), this works for me:
$mail->personalization[0]->addSubstitution("{{contractId}}", (string)$someIntegerId);

Same thing started happening for me earlier this year on python...

Ended up changing
mail.add_custom_arg(CustomArg(key=key, value=value))
to
mail.add_custom_arg(CustomArg(key=key, value=str(value)))
to prevent HTTP/1.1 400 Bad Request errors...

This has been moved here.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

solonifer picture solonifer  路  3Comments

moontrv picture moontrv  路  3Comments

morazain picture morazain  路  3Comments

KayakinKoder picture KayakinKoder  路  5Comments

jverlee picture jverlee  路  4Comments