Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error code 400: "Bad Request" when adding custom arguments as Integer values #350

Closed
igorokb opened this issue Jan 24, 2017 · 6 comments
Closed
Labels
status: help wanted requesting help from the community type: bug bug in the library

Comments

@igorokb
Copy link

igorokb commented Jan 24, 2017

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

@thinkingserious thinkingserious added status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap labels Jan 24, 2017
@thinkingserious
Copy link
Contributor

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.

@nelsounet
Copy link

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.""));
}

@thinkingserious
Copy link
Contributor

@nelsounet,

Can you please post your payload?

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

Thanks!

@danielsippel
Copy link

+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);

@thinkingserious thinkingserious added type: bug bug in the library and removed type: community enhancement feature request not on Twilio's roadmap labels Apr 28, 2017
@MihailRussu
Copy link

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...

@thinkingserious
Copy link
Contributor

This has been moved here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted requesting help from the community type: bug bug in the library
Projects
None yet
Development

No branches or pull requests

5 participants