Subscribing to a brevo list does not work if the contact already exists: webform does not display any error, but the message "RequestException: Input must be a valid JSON object, bad_request" appears in the Drupal log

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mdruilhe created an issue. See original summary.

mdruilhe’s picture

Issue summary: View changes

mrshowerman’s picture

Assigned: mdruilhe » Unassigned
Priority: Normal » Major
Status: Active » Needs review

I have the same error, and the patch from the MR fixes it.
Left a nit on the MR. Raised the priority, because I think it is a real problem if existing users can't subscribe to another list.

szato’s picture

Got the same error, plus:
RequestException: Invalid list id, invalid_parameter

The implemented change fixed my issue for subscribing to a same list if contact already exists.

Using module 1.2.3 version with getbrevo/brevo-php v2.0.14.

All these API calls:
addContactToList()
removeContactFromList()

should have $body (the contact object) as the first argument and $listId as the second argument, but parameters are swapped causing the API to receive the list ID in the body parameter position and vice versa, resulting in the "invalid_parameter" error from Brevo's API.

tostinni made their first commit to this issue’s fork.

tostinni’s picture

On top of the argument-order fix already in this MR, this also handles the "already subscribed" case that this issue is titled after.

When subscribing a contact that is already in the target list, Brevo answers addContactToList() with HTTP 400 / code "invalid_parameter" and the message "Contact already in list and can't be added again". handleRequestException() turns that into a form error, so a returning subscriber sees a failure even though the contact already is in the desired state (subscribed).

This change adds an isContactAlreadyInListException() helper that inspects the API error body (code == invalid_parameter + "contact already in list" in the message) and, on the two addContactToList() call sites, skips handleRequestException() for that specific case only — every other error is still surfaced as before.

No behavior change for genuine failures.